【问题标题】:NPOI Excel number format not showing in Excel sheet in asp.netNPOI Excel 数字格式未显示在 asp.net 的 Excel 工作表中
【发布时间】:2010-08-06 16:50:25
【问题描述】:

我正在尝试使用 NPOI 库在 excel 中创建双精度和数字格式的单元格。我使用了类似的代码

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

在 excel 中数字是正确对齐的,但是当我检查格式时它显示在“常规”中

然后我将代码更改为如下所示

 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

然后在打开文件时出现错误并且需要很长时间才能打开。但 Excel 格式显示在“数字”中

错误显示如下。

如何解决这个问题?

【问题讨论】:

标签: c# asp.net vb.net excel npoi


【解决方案1】:

看看this,你是在为每个单元格创建一个cellStyle 对象吗?如果是这样不要。在创建单元格之前尝试只创建几个样式,然后将这些预定义样式应用于您创建的单元格。

【讨论】:

    【解决方案2】:

    要修复太多不同的单元格样式,请在您可能正在运行的任何循环之外声明所有样式。

    我假设你 'j' 将是枚举数,所以我会以更正的格式删除你所拥有的内容。

    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
    

    通过限制“新”样式的数量,这应该会更好一些。

    【讨论】:

      【解决方案3】:

      Hare 是一种在 Excel 文档中创建双重格式的简单方法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.
      

      这是工作版本,我已经用过很多项目了。

      很难在 Excel 中插入 DateTime 格式,互联网上没有很好的例子,我认为它可以帮助人们以正确的方式做到这一点。 我给你看代码示例:

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

      var eRow = sheet.CreateRow(rowIndex); //创建新行 // rowIndex - 它是整数,例如: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
      

      【讨论】:

      • workBook.CreateDataFormat().GetFormat();确实是正确的方法,GetBuiltinFormat() 没有运气
      • 我已经完成了解决方案,它使用这种方式来制作正确的日期时间格式,此时解决方案可以正常工作。我认为这是最好的方法。
      【解决方案4】:

      然后为列创建一个样式,但这个样式

       ICellStyle _TextCellStyle = wb1.CreateCellStyle();
      
       _TextCellStyle.DataFormat = wb1.CreateDataFormat().GetFormat("@");
       sheet.SetDefaultColumnStyle(2, _TextCellStyle);
      

      【讨论】:

        猜你喜欢
        • 2013-03-10
        • 1970-01-01
        • 2021-07-03
        • 1970-01-01
        • 2017-07-11
        • 1970-01-01
        • 2014-01-23
        • 2014-01-15
        • 1970-01-01
        相关资源
        最近更新 更多