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();
}
}
}
運行后,效果如下圖所示【演示了不同數字格式的設置】

轉載:http://blog.csdn.net/xxs77ch/article/details/50237017
