找到dll文件引用
using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.IO; namespace NPOI.WriteExcel { class Program { /*NPOI.DLL:NPOI 核心函式庫。 NPOI.DDF.DLL:NPOI 繪圖區讀寫函式庫。 NPOI.HPSF.DLL:NPOI 文件摘要資訊讀寫函式庫。 NPOI.HSSF.DLL:NPOI Excel BIFF 檔案讀寫函式庫。 NPOI.Util.DLL:NPOI 工具函式庫。 NPOI.POIFS.DLL:NPOI OLE 格式存取函式庫。 ICSharpCode.SharpZipLib.DLL:檔案壓縮函式庫。 */ static void Main(string[] args) { new Program().CreateExcel(); new Program().InsertContext(); } /// <summary> /// 创建Excel /// </summary> private void CreateExcel() { HSSFWorkbook workbook = new HSSFWorkbook(); FileStream filestream = new FileStream(AppDomain.CurrentDomain.BaseDirectory+DateTime.Now.ToString("yyyyMMddHHmmss")+".xls",FileMode.Create); // 新增試算表。 workbook.CreateSheet("試算表 A"); workbook.CreateSheet("試算表 B"); workbook.CreateSheet("試算表 C"); workbook.Write(filestream); workbook.Close(); filestream.Close(); filestream.Dispose(); } /// <summary> /// 像Excel中添加内容 /// </summary> private void InsertContext() { HSSFWorkbook workbook = new HSSFWorkbook(); FileStream filestream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls", FileMode.Create); // 新增試算表。 ISheet sheet1 = workbook.CreateSheet("試算表 A"); ISheet sheet2 = workbook.CreateSheet("試算表 B"); ISheet sheet3 = workbook.CreateSheet("試算表 C"); //依次创建行和列 for (int i = 0; i < 10; i++) { IRow row1 = sheet1.CreateRow(i); IRow row2 = sheet2.CreateRow(i); IRow row3 = sheet3.CreateRow(i); for (int j = 0; j < 10; j++) { ICell cell1 = row1.CreateCell(j); cell1.SetCellValue("第" + (i + 1) + "行,第" + (j + 1) + "列"); ICell cell2 = row2.CreateCell(j); cell2.SetCellValue("第" + (i + 1) + "行,第" + (j + 1) + "列"); ICell cell3 = row3.CreateCell(j); cell3.SetCellValue("第" + (i + 1) + "行,第" + (j + 1) + "列"); } } workbook.Write(filestream); workbook.Close(); filestream.Close(); filestream.Dispose(); } } }
应用中。。。。。
HSSFWorkbook workbook = new HSSFWorkbook(); FileStream filestream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls", FileMode.Create); // 新增試算表。 ISheet sheet1 = workbook.CreateSheet("試算表 A"); ISheet sheet2 = workbook.CreateSheet("試算表 B"); ISheet sheet3 = workbook.CreateSheet("試算表 C"); //创建表头 IRow rowHead = sheet1.CreateRow(0); rowHead.CreateCell(0).SetCellValue("序号"); sheet1.SetColumnWidth(0, 256 * 15); rowHead.CreateCell(1).SetCellValue("QQ"); sheet1.SetColumnWidth(1, 256 * 25); rowHead.CreateCell(2).SetCellValue("名称"); sheet1.SetColumnWidth(2, 256 * 20); rowHead.CreateCell(3).SetCellValue("时间"); sheet1.SetColumnWidth(3, 256 * 15); //依次填充值 string beginTime = this.dateTimePicker1.Value.ToString().Split(' ')[0]; string endTime = this.dateTimePicker2.Value.ToString(); pagesize = Convert.ToInt32(this.label4.Text); DataSet ds = bll.GetList("", beginTime, endTime, Inum, pagesize, out allCount); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { //创建第二行,并赋值 IRow rowHead2 = sheet1.CreateRow(i+1); rowHead2.CreateCell(0).SetCellValue(ds.Tables[0].Rows[i][0].ToString()); rowHead2.CreateCell(1).SetCellValue(ds.Tables[0].Rows[i][1].ToString()); rowHead2.CreateCell(2).SetCellValue(ds.Tables[0].Rows[i][2].ToString()); rowHead2.CreateCell(3).SetCellValue(ds.Tables[0].Rows[i][3].ToString()); } workbook.Write(filestream); workbook.Close(); filestream.Close(); filestream.Dispose();