Question

I am trying to create double and number format cells in excel using NPOI library. I used code like

Dim cell As HSSFCell = row.CreateCell(j)
cell.SetCellValue(Double.Parse(dr(col).ToString))

In excel numbers are aligning right but when I check format it is showing in "General"

alt text

then I changed my code to like below

 Dim cell As HSSFCell = row.CreateCell(j)
 cell.SetCellValue(Double.Parse(dr(col).ToString))
 Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
 cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,#0.0")
 cell.CellStyle = cellStyle

Then While opening file it is giving error and also taking so long to open. But Excel format showing in "Number"

error showing is like below.

alt text

How to fix this?

Was it helpful?

Solution

Take a look at this, are you creating a cellStyle object for each cell? If so don't. Try creating just a couple of styles before creating your cells and then apply these pre-defined styles to the cells you create.

OTHER TIPS

Hare is a simple way to create double format in Excel Document USING NPOI.

//make NUMERIC Format in Excel Document // Author: Akavrelishvili
  var eRow = sheet.CreateRow(rowIndex); //create new Row , rowIndex - it's integer, like : 1,2,3
  eRow.CreateCell(0).SetCellValue(row["ProvidName"].ToString()); //create cell and set string value

  double Amount = Convert.ToDouble(row["Amount"].ToString()); //convert string to double
  eRow.CreateCell(1).SetCellValue(Amount); // create cell and set double value.

This is working version, I have used it a lots of projects.

Very Hard is to insert DateTime format in Excel, There no good example in Internet and I think it helps people to do it right way. I show you code example:

     //make Date Time Format in Excel Document // Author: Akavrelishvili

var eRow = sheet.CreateRow(rowIndex); //create new Row // rowIndex - it's integer, like : 1,2,3

 ICellStyle cellDateStyle = workBook.CreateCellStyle(); //create custom style
 cellDateStyle.DataFormat = workBook.CreateDataFormat().GetFormat("dd/mm/yyyy"); //set day time Format

 eRow.CreateCell(3).SetCellValue(Convert.ToDateTime(row["Date"])); //set DateTime value to cell
                    eRow.GetCell(6).CellStyle = cellDateStyle; // Restyle cell using "cellDateStyle"


I hope it helps

To fix the too many different cell styles declare all styles outside of any loop you may be running.

I'm presumeing you 'j' would be the enumerator so i'll drop what you had in a corrected format for you.

Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,#0.0")

For col = 0 To ColoumCounter
  For j = 0 To Counter
    Dim cell As HSSFCell = row.CreateCell(j)
    cell.SetCellValue(Double.Parse(dr(col).ToString))
    cell.CellStyle = cellStyle
  Next
Next

This should work a bit better, by limiting the number of "New" styles.

Create a style then but this style for the column

 ICellStyle _TextCellStyle = wb1.CreateCellStyle();

 _TextCellStyle.DataFormat = wb1.CreateDataFormat().GetFormat("@");
 sheet.SetDefaultColumnStyle(2, _TextCellStyle);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top