使用NPOI生成Excel文件的字段一直都是自定义类型,不是数值型;代码如下:
//----------------普通--3位------------
ICellStyle hStyleGrid_Three = workbook.CreateCellStyle();
hStyleGrid_Three.Alignment = HorizontalAlignment.Right;
//设置边框
hStyleGrid_Three.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
hStyleGrid_Three.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
hStyleGrid_Three.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
hStyleGrid_Three.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
hStyleGrid_Three.DataFormat = format.GetFormat("0.000");
hStyleGrid_Three.SetFont(fGrid);
这种生成的Excel 文件的字段类型一直都是自定义类型,如下图:
![](https://i-blog.csdnimg.cn/blog_migrate/49da3967cc7ed466f0d7c3e27dd02ac4.png)
后来,研究了好久,才发现原来是格式错了,不能这么写 format.GetFormat("0.000");需要这样写:format.GetFormat("0.000_);[Red]\\(0.000\\)"); 片段代码如下:
//----------------普通--3位------------
ICellStyle hStyleGrid_Three = workbook.CreateCellStyle();
//设置边框
hStyleGrid_Three.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
hStyleGrid_Three.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
hStyleGrid_Three.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
hStyleGrid_Three.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
hStyleGrid_Three.Alignment = HorizontalAlignment.Right;
hStyleGrid_Three.SetFont(fGrid);
hStyleGrid_Three.DataFormat = format.GetFormat("0.000_);[Red]\\(0.000\\)"); //小数点3位
修改后,NOPI生成的Excel的字段为:数值型了,如下图:
![](https://i-blog.csdnimg.cn/blog_migrate/bcaa8a0ff77ea08c9ed027ad6fa10d77.png)