我试图创建使用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格式表示“编号”

误差表示为象下面。

“替代文字”

如何解决这一问题?

有帮助吗?

解决方案

看看这个,你在创建每个小区的cellStyle对象?如果这样做不行。尝试创建细胞之前创建只是一对夫妇的样式,然后将这些预定义的样式来创建细胞。

其他提示

野兔是一种简单的方法来创建在Excel文档双格式的使用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

变种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

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

我presumeing你“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

这应该工作好一点,通过限制“新”样式的数量。

创建的风格,那么但这种风格的列

 ICellStyle _TextCellStyle = wb1.CreateCellStyle();

 _TextCellStyle.DataFormat = wb1.CreateDataFormat().GetFormat("@");
 sheet.SetDefaultColumnStyle(2, _TextCellStyle);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top