使用NPOI设置Excel单元格格式

在项目开发经常需要读写excel,可以使用OleDb读写Excel,但是由于Excel不是数据库,字段的类型不是事先定义好的,在更新单元格内容时,总是提示“标准表达式中数据类型不匹配”,原因时单元格格式问题 。需要在执行更新脚本前,批量修改列的单元格格式。
使用NPOI可以在不关心客户端电脑Office版本的情况下,更新Excel单元格格式,代码如下:

        public void ModifySheetCellFormat()
        {
            #region 打开Excel表格模板,并初始化到NPOI对象中
            IWorkbook wk = null;
            string extension = System.IO.Path.GetExtension(sExcelFilename);
            FileStream fs = File.OpenRead(sExcelFilename);
            if (extension.Equals(".xls"))
                wk = new HSSFWorkbook(fs);
            else
                wk = new XSSFWorkbook(fs);
            fs.Close();
            #endregion
            //List<string> SheetNames = new List<string>();
            //for (int i = 0; i < wk.NumberOfSheets; i++)
            //{
            //    SheetNames.Add(wk.GetSheetName(i));
            //}
            string sheetname = SelectedSheetName.Substring(1, SelectedSheetName.Length - 3);
            ISheet sheet = wk.GetSheet(sheetname);
            IRow row = null;//数据行
            ICell cell = null;//数据行中的某列
            for (int i = 0; i < sheet.PhysicalNumberOfRows; i++)
            {
                row = sheet.GetRow(i);
                cell = row.GetCell(1);
                IDataFormat dataformat = wk.CreateDataFormat();
                ICellStyle style0 = wk.CreateCellStyle();
                style0.DataFormat = dataformat.GetFormat("@");//转化为汉字大写
                cell.CellStyle = style0;

                cell = row.GetCell(52);
                IDataFormat dataformat2 = wk.CreateDataFormat();
                ICellStyle style1 = wk.CreateCellStyle();
                style1.DataFormat = dataformat.GetFormat("@");//转化为汉字大写
                cell.CellStyle = style1;
            }

            FileStream file = new FileStream(sExcelFilename, FileMode.Create);
            wk.Write(file);
            file.Close();
        }

NPOI定义单元格格式有以下种类(参考链接):

            IDataFormat dataformat = wk.CreateDataFormat();
 
            ICellStyle style0 = wk.CreateCellStyle();
            style0.DataFormat = dataformat.GetFormat("[DbNum2][$-804]General");//转化为汉字大写
 
            ICellStyle style1 = wk.CreateCellStyle();
            style1.DataFormat = dataformat.GetFormat("0.0"); //改变小数精度【小数点后有几个0表示精确到小数点后几位】
 
            ICellStyle style2 = wk.CreateCellStyle();
            style2.DataFormat = dataformat.GetFormat("#,##0.0");//分段添加,号
 
            ICellStyle style3 = wk.CreateCellStyle();
            style3.DataFormat = dataformat.GetFormat("0.00E+00");//科学计数法
 
            ICellStyle style4 = wk.CreateCellStyle();
            style4.DataFormat = dataformat.GetFormat("0.00;[Red]-0.00");//正数与负数的区分
 
            ICellStyle style5 = wk.CreateCellStyle();
            style5.DataFormat = dataformat.GetFormat("# ??/??");//整数部分+分数
 
            ICellStyle style6 = wk.CreateCellStyle();
            style6.DataFormat = dataformat.GetFormat("??/??");//分数
 
            ICellStyle style7 = wk.CreateCellStyle();
            style7.DataFormat = dataformat.GetFormat("0.00%");//百分数【小数点后有几个0表示精确到显示小数点后几位】
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值