网上有很多关于NPOI中数字小数位格式的设置,常见的有
HSSFDataFormat.GetBuiltinFormat("0.00");
本人要根据具体的列来设置具体的小数位,上面的就没有用了,网上还有一种
ICellStyle numberStyle1 = workbook.CreateCellStyle();
numberStyle1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER;
numberStyle1.DataFormat = format1.GetFormat("0.0000");
上面的写法如果是统一的列可以把numberStyle1 写在模块外面,如果要对多列进行就必须有多个numberStyle1 ,具体如下
if(k==5|| k == 9)
{ //当要设置多个单元格样式必须创建多个单元格样式实例
ICellStyle numberStyle1 = workbook.CreateCellStyle();
numberStyle1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER;
numberStyle1.DataFormat = format1.GetFormat("0.0000");
cell.CellStyle = numberStyle1;
}
else if (k == 7)
{
ICellStyle numberStyle1 = workbook.CreateCellStyle();
numberStyle1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER;
numberStyle1.DataFormat = format1.GetFormat("0.000");
cell.CellStyle = numberStyle1;
}
看完你或许会说直接将numberStyle1.DataFormat每次覆盖掉,但是实际上本人实际项目实验了几遍都不行