2

我正在尝试使用 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 格式显示在“数字”中

错误显示如下。

替代文字

如何解决这个问题?

4

4 回答 4

4

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

于 2010-08-06T17:05:07.370 回答
1

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

我假设你'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

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

于 2015-04-23T12:14:19.227 回答
0

Hare 是一种使用 NPOI在 Excel 文档中创建双重格式的简单方法。

//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
于 2014-12-04T10:46:31.057 回答
0

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

 ICellStyle _TextCellStyle = wb1.CreateCellStyle();

 _TextCellStyle.DataFormat = wb1.CreateDataFormat().GetFormat("@");
 sheet.SetDefaultColumnStyle(2, _TextCellStyle);
于 2018-03-08T08:54:24.263 回答