locked
NPOI Excel number format not showing in Excel sheet in asp.net RRS feed

  • Question

  • User645293055 posted

    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"


    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.

     

    Friday, August 6, 2010 12:55 PM

All replies

  • User1508394307 posted

    Try

    Dim cell As HSSFCell = row.CreateCell(j)  
    Dim d As Double = Convert.ToDouble(dr(col))
    cell.CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00")

    See all builtin formats here.

    Doing 

    cell.CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("General")

    should also work I think, but in this case to make proper format you would probably need to format double value as a string in a format you want and set it to the cell. 

    Saturday, August 7, 2010 5:33 AM