• NPOI设定单元格格式(数值型插入)


    以下参考:https://www.e-learn.cn/topic/641548

     IDataFormat dataformat = myworkbook.CreateDataFormat();
       ICellStyle style0 = myworkbook.CreateCellStyle();
                style0.DataFormat = dataformat.GetFormat("[DbNum2][$-804]General");//转化为汉字大写
    
                ICellStyle style1 = myworkbook.CreateCellStyle();
                style1.DataFormat = dataformat.GetFormat("0.0"); //改变小数精度【小数点后有几个0表示精确到小数点后几位】
    
                ICellStyle style2 = myworkbook.CreateCellStyle();
                style2.DataFormat = dataformat.GetFormat("#,##0.0");//分段添加,号
    
                ICellStyle style3 = myworkbook.CreateCellStyle();
                style3.DataFormat = dataformat.GetFormat("0.00E+00");//科学计数法
    
                ICellStyle style4 = myworkbook.CreateCellStyle();
                style4.DataFormat = dataformat.GetFormat("0.00;[Red]-0.00");//正数与负数的区分
    
                ICellStyle style5 = myworkbook.CreateCellStyle();
                style5.DataFormat = dataformat.GetFormat("# ??/??");//整数部分+分数
    
                ICellStyle style6 = myworkbook.CreateCellStyle();
                style6.DataFormat = dataformat.GetFormat("??/??");//分数
    
                ICellStyle style7 = myworkbook.CreateCellStyle();
                style7.DataFormat = dataformat.GetFormat("0.00%");//百分数【小数点后有几个0表示精确到显示小数点后几位】
                
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    hStyleGrid_Three.DataFormat = format.GetFormat(“0.000”);
    得到的结果是:(自定义类型)

    在这里插入图片描述
    后来,研究了好久,才发现原来是格式错了,不能这么写 format.GetFormat(“0.000”);需要这样写:format.GetFormat(“0.000_);[Red]0.0000.000”); 片段代码如下:
    hStyleGrid_Three.DataFormat = format.GetFormat(“0.000_);[Red]0.0000.000”); //小数点3位

    修改后,NOPI生成的Excel的字段为:数值型了,如下图: 数字类型
    在这里插入图片描述
    但是只是修改数据单元格类型还是不可以实现值得导出求和,所以需要把数据类型直接放到cell内
    在这里插入图片描述

    根据:

        void SetCellValue(bool value);
        void SetCellValue(string value);
        void SetCellValue(IRichTextString value);
        void SetCellValue(DateTime value);
        void SetCellValue(double value);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    我的代码:根据类型放入不一样的值:

     switch (valueType)
     {
                                        case "System.Decimal":
                                            //str = Convert.ToString(Math.Round(Convert.ToDecimal(itemRow.Cells[itemColumn.Index].Value), 2));
                                            Double DB = (double)Math.Round(Convert.ToDecimal(itemRow.Cells[itemColumn.Index].Value), 2);
                                            cellStyle2.DataFormat = dataformat.GetFormat("0.00_");
                                            cell1.SetCellValue(DB);
                                            //cell1.SetCellValue(str);
                                            break;
                                        case "System.Double":
                                            //str = Convert.ToString(Math.Round(Convert.ToDouble(itemRow.Cells[itemColumn.Index].Value), 2));
                                            Double DBD = (double)Math.Round(Convert.ToDecimal(itemRow.Cells[itemColumn.Index].Value), 2);
                                            cellStyle2.DataFormat = dataformat.GetFormat("0.00_");
                                            cell1.SetCellValue(DBD);
                                            break;
                                        case "System.DateTime":
                                            str = Convert.ToDateTime(itemRow.Cells[itemColumn.Index].Value).ToString("yyyy-MM-dd");// HH:mm:ss");
                                            cell1.SetCellValue(str);
                                            break;
                                        case "System.Int32":
                                            Double DBInt = (double)Math.Round(Convert.ToDecimal(itemRow.Cells[itemColumn.Index].Value), 2);
                                            cellStyle2.DataFormat = dataformat.GetFormat("0.00_");
                                            cell1.SetCellValue(DBInt);
                                            //cell1.SetCellValue(str);
                                            break;
                                        default:
                                            str = Convert.ToString(itemRow.Cells[itemColumn.Index].Value ?? "");
                                            //cell1.SetCellValue(str.ToString());
                                            cell1.SetCellValue(str);
                                            break;
                                    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    差别立即不同
    在这里插入图片描述

  • 相关阅读:
    不只有 Spring,这四款 Java 基础开发框架同样值得关注!
    Java基础入门·File类的使用
    Redis实现分布式锁-原理-问题详解
    电商平台搭建流程是怎样的_不懂编程怎么搭建_OctShop
    安全狗陈奋:数据安全需要建立在传统网络安全基础之上
    传参base64时的+号变空格问题
    前端面试系列之工程化篇
    URLDNS反序列化链学习
    【ELFK】之zookeeper
    【Vue3.0移动端项目--旅游网】-- 房东评价、热门评论、预定须知模块
  • 原文地址:https://blog.csdn.net/weixin_40029679/article/details/127755573