使用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 文件的字段类型一直都是自定义类型,如下图:

C# 使用 NPOI导出Excel 字段为数值型
自定义类型

后来,研究了好久,才发现原来是格式错了,不能这么写 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的字段为:数值型了,如下图:

C# 使用 NPOI导出Excel 字段为数值型
数字类型

 

 

相关文章:

  • 2021-07-02
  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-27
猜你喜欢
  • 2021-08-04
  • 2022-12-23
  • 2021-05-23
  • 2022-02-03
  • 2022-02-09
  • 2022-01-20
相关资源
相似解决方案