NPOI2.2.0.0实例详解(八)—设置EXCEL单元格【数字格式】
作者:互联网
原文链接:https://my.oschina.net/u/1778848/blog/542247
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Eval;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.POIFS.FileSystem;
using NPOI.HPSF;
using System.IO;
using NPOI.SS.Util;
using System.Drawing;
using NPOI.HSSF.Util;
namespace NPOI
{
class Program7
{
static void Main(string[] args)
{
//说明:设置数字格式
//1.创建EXCEL中的Workbook
IWorkbook myworkbook = new XSSFWorkbook();
//2.创建Workbook中的Sheet
ISheet mysheet = myworkbook.CreateSheet("sheet1");
mysheet.SetColumnWidth(0, 20 * 256);
mysheet.SetColumnWidth(1, 20 * 256);
//3.创建Row中的Cell并赋值
IRow row0 = mysheet.CreateRow(0); row0.CreateCell(0).SetCellValue(2013.143); row0.CreateCell(1).SetCellValue("转化为汉字大写");
IRow row1 = mysheet.CreateRow(1); row1.CreateCell(0).SetCellValue(123152013.143); row1.CreateCell(1).SetCellValue("改变小数精度");
IRow row2 = mysheet.CreateRow(2); row2.CreateCell(0).SetCellValue(123152013.143); row2.CreateCell(1).SetCellValue("分段添加,号");
IRow row3 = mysheet.CreateRow(3); row3.CreateCell(0).SetCellValue(123152013.143); row3.CreateCell(1).SetCellValue("科学计数法");
IRow row4 = mysheet.CreateRow(4); row4.CreateCell(0).SetCellValue(-123152013.143); row4.CreateCell(1).SetCellValue("正数与负数的区分(负数红色)");
IRow row5 = mysheet.CreateRow(5); row5.CreateCell(0).SetCellValue(123152013.77); row5.CreateCell(1).SetCellValue("整数部分+分数");
IRow row6 = mysheet.CreateRow(6); row6.CreateCell(0).SetCellValue(123152013.77); row6.CreateCell(1).SetCellValue("分数");
IRow row7 = mysheet.CreateRow(7); row7.CreateCell(0).SetCellValue(0.333); row7.CreateCell(1).SetCellValue("百分数");
//4.创建CellStyle与DataFormat并加载格式样式
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表示精确到显示小数点后几位】
//5.将CellStyle应用于具体单元格
row0.GetCell(0).CellStyle = style0;
row1.GetCell(0).CellStyle = style1;
row2.GetCell(0).CellStyle = style2;
row3.GetCell(0).CellStyle = style3;
row4.GetCell(0).CellStyle = style4;
row5.GetCell(0).CellStyle = style5;
row6.GetCell(0).CellStyle = style6;
row7.GetCell(0).CellStyle = style7;
//6.保存
FileStream file = new FileStream(@"E:\myworkbook7.xlsx", FileMode.Create);
myworkbook.Write(file);
file.Close();
}
}
}
运行后,效果如下图所示【演示了不同数字格式的设置】
转载于:https://my.oschina.net/u/1778848/blog/542247
标签:mysheet,CellStyle,NPOI2.2,0.0,单元格,CreateCell,myworkbook,using,SetCellValue 来源: https://blog.csdn.net/choujing2591/article/details/100809111