ASP.NET - How to set format to cell in Excel? - Asked By Kerry Warret on 21-Sep-13 10:34 PM

I have known to set value to cell in Excel, but I have not know to set number format to the cell.
for example I need to 12 show 12.00%, How to do using Acey.ExcelX .

This is my code.

using Acey.ExcelX

     
IWorkbook workbook = ExcelxApplication.CreateWorkbook();

IWorksheet worksheet = workbook.Worksheets[0];

ICell cell = worksheet.Cells[0, 0];

cell.Value = 12;

 workbook.SaveAs(@"d:\book1.xls", FileFormat.Excel97To2003);


If you know, please help me, Thanks.





Tom Jashon replied to Kerry Warret on 22-Sep-13 01:08 AM
ConsoleApplication5.zip

hi https://www.nullskull.com/forumtree.aspx?userID=f2b3bdab-56ba-4ad0-9612-58e4a49f1417

you need to set format to the specified cell what you want, Acey.ExcelX provides interface name: ICellFormat,
this interface can set format for a single cell, a range, entire row and column, you can see CellFormat property in
ICell interface. you write and test it, it works fine.

the following code:

        //Creates a Workbook object.      
         IWorkbook workbook = ExcelxApplication.CreateWorkbook();
        //Gets a worksheet object by zero-based index in workbook.
         IWorksheet worksheet = workbook.Worksheets[0];
      //Gets a cell by zero-based row and column index.
        ICell cell = worksheet.Cells[0, 0];
        //Sets value to the Cell object.
        cell.Value = 12;
        //Sets the number format
        cell.CellFormat.NumberFormat = "0.00%";

        //Saves as the sepcified excel file format.
        workbook.SaveAs(@"d:\book1.xls", FileFormat.Excel97To2003);

you can download the attached file.

regards