本文转载自:http://blog.csdn.net/xxs77ch/article/details/50245641
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 Program9 { static void Main(string[] args) { //说明:设置文本格式 //1.创建EXCEL中的Workbook IWorkbook myworkbook = new XSSFWorkbook(); //2.创建Workbook中的Sheet ISheet mysheet = myworkbook.CreateSheet("sheet1"); mysheet.SetColumnWidth(0, 40 * 256); //3.创建Row中的Cell并赋值 IRow row0 = mysheet.CreateRow(0); row0.CreateCell(0).SetCellValue("130925199662080044"); IRow row1 = mysheet.CreateRow(1); row1.CreateCell(0).SetCellValue(""+DateTime.Now+""); //4.创建CellStyle与DataFormat并加载格式样式 IDataFormat dataformat = myworkbook.CreateDataFormat(); //【Tips】 // 1.使用@ 或 text 都可以 // 2.再也不用为身份证号发愁了 ICellStyle style0 = myworkbook.CreateCellStyle(); style0.DataFormat = dataformat.GetFormat("@"); ICellStyle style1 = myworkbook.CreateCellStyle(); style1.DataFormat = dataformat.GetFormat("text"); //5.将CellStyle应用于具体单元格 row0.GetCell(0).CellStyle = style0; row1.GetCell(0).CellStyle = style1; //6.保存 FileStream file = new FileStream(@"E:myworkbook9.xlsx", FileMode.Create); myworkbook.Write(file); file.Close(); } } }