摘要:使用開源程序NPOI導出Excel示例。NPOI首頁地址:http://npoi.codeplex.com/,NPOI示例博客:http://tonyqus.sinaapp.com/。c++
示例編寫環境:app
操做系統:Windows7 Ultimate SP1 64bit函數
開發軟件:Visual Studio Ultimate 2012 Update 3字體
Office版本:Office Home and Student 2010ui
NPOI版本:NPOI 2.0spa
示例界面以下:操作系統
本示例中使用到的命名空間:excel
1 using System; 2 using System.Data; 3 using System.Web; 4 using System.Web.UI; 5 using System.Collections.Generic; 6 using System.Web.UI.WebControls; 7 using System.IO; 8 using System.Text; 9 using NPOI.SS.UserModel; 10 using NPOI.HSSF.UserModel; 11 using NPOI.HSSF.Util; 12 using NPOI.HPSF; 13 using NPOI.POIFS.FileSystem; 14 using NPOI.SS.Util;
導出按鈕代碼:code
1 protected void btnExportClick(object sender, EventArgs e) 2 { 3 // 文件名最好使用英文,中文可能會出現亂碼或者丟失文件後綴 4 string filename = "ReportExport" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); 5 Response.Clear(); 6 Response.ContentType = "application/vnd.ms-excel"; 7 Response.AppendHeader("Content-Type", "applicationnd.ms-excel"); 8 Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls"); 9 10 // getData()函數返回一個DataTable類型,在本實例中用於得到要導出的數據,可根據項目狀況替換該函數 11 Response.BinaryWrite(getExportContent(getData()).GetBuffer()); 12 Response.Flush(); 13 // 若使用Response.End方法會致使ThreadAbortException 異常,使用下面的語句替換Response.End方法 14 HttpContext.Current.ApplicationInstance.CompleteRequest(); 15 }
負責生成導出信息的方法getExportContent示例代碼以下:orm
1 private MemoryStream getExportContent(DataTable table) 2 { 3 // 添加Excel元數據信息 4 HSSFWorkbook book = new HSSFWorkbook(); 5 DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); 6 dsi.Company = "MyCompany"; 7 book.DocumentSummaryInformation = dsi; 8 9 SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); 10 si.Subject = "MySubject"; 11 book.SummaryInformation = si; 12 13 // 聲明必要的變量 14 MemoryStream ms = new MemoryStream(); 15 ISheet sheet = book.CreateSheet("sheet1"); 16 IRow erow = sheet.CreateRow(0); 17 18 // 設置列寬,列寬的單位是256分之一字符,因此10*256表示10個字符寬度 19 sheet.SetColumnWidth(1, 20 * 256); 20 sheet.SetColumnWidth(2, 20 * 256); 21 sheet.SetColumnWidth(3, 10 * 256); 22 sheet.SetColumnWidth(4, 10 * 256); 23 24 // 增長表頭 25 buildSheetTitle(book); 26 27 // 增長內容,標題佔兩行,從第三行添加數據內容。 28 // 在NPOI中行列以0做爲起始 29 int rindex = 2; 30 foreach (DataRow r in table.Rows) 31 { 32 erow = sheet.CreateRow(rindex); 33 // 序號 34 erow.CreateCell(0).SetCellValue(rindex - 1); 35 for (int c = 0; c < table.Columns.Count; c++) 36 { 37 erow.CreateCell(c + 1).SetCellValue(r[c].ToString()); 38 } 39 rindex++; 40 } 41 // 對相同的列進行縱向合併 42 mergeColumn(book); 43 44 45 book.Write(ms); 46 ms.Flush(); 47 ms.Position = 0; 48 return ms; 49 }
增長表頭方法buildSheetTitle示例代碼:
1 private void buildSheetTitle(HSSFWorkbook book) 2 { 3 // 定義必要變量 4 ISheet sheet = book.GetSheetAt(0); 5 IRow erow = sheet.CreateRow(0); 6 ICell cell = null; 7 // 單元格樣式 8 ICellStyle style = book.CreateCellStyle(); 9 style.Alignment = HorizontalAlignment.CENTER; 10 style.VerticalAlignment = VerticalAlignment.CENTER; 11 IFont font = book.CreateFont(); 12 font.FontHeightInPoints = 12; 13 font.FontName = "宋體"; 14 style.SetFont(font); 15 16 // 增長第一行標題 17 cell = erow.CreateCell(0); 18 cell.SetCellValue("序號"); 19 cell.CellStyle = style; 20 sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 0)); 21 22 cell = erow.CreateCell(1); 23 cell.SetCellValue("裝置名稱"); 24 cell.CellStyle = style; 25 sheet.AddMergedRegion(new CellRangeAddress(0, 1, 1, 1)); 26 27 cell = erow.CreateCell(2); 28 cell.SetCellValue("物料信息"); 29 cell.CellStyle = style; 30 sheet.AddMergedRegion(new CellRangeAddress(0, 0, 2, 3)); 31 32 // 增長第二行標題 33 erow = sheet.CreateRow(1); 34 cell = erow.CreateCell(2); 35 cell.SetCellValue("物料名稱"); 36 cell.CellStyle = style; 37 cell = erow.CreateCell(3); 38 cell.SetCellValue("小計"); 39 cell.CellStyle = style; 40 }
合併單元格方法mergeColumn示例代碼以下:
1 private void mergeColumn(HSSFWorkbook book) 2 { 3 ISheet sheet = book.GetSheetAt(0); 4 int cunit = 1; 5 // 合併單元格跨越的行數 6 int rspan = 0; 7 // 需合併單元格的起始行,前兩行爲標題,默認起始行是第三行 8 int srow = 2; 9 10 // 單元格字體樣式 11 IFont font = book.CreateFont(); 12 font.FontHeightInPoints = 12; 13 font.FontName = "宋體"; 14 15 // 非合併單元格樣式 16 ICellStyle style = book.CreateCellStyle(); 17 style.SetFont(font); 18 19 // 合併單元格樣式 20 ICellStyle mstyle = book.CreateCellStyle(); 21 mstyle.Alignment = HorizontalAlignment.CENTER; 22 mstyle.VerticalAlignment = VerticalAlignment.CENTER; 23 mstyle.SetFont(font); 24 25 26 // 合併單元格 27 string oldvalue = string.Empty; 28 for (int r = 2; r < sheet.PhysicalNumberOfRows; r++) 29 { 30 if (sheet.GetRow(r).GetCell(cunit).StringCellValue == oldvalue) 31 { 32 rspan++; 33 } 34 else 35 { 36 // 添加合併區域 37 sheet.AddMergedRegion(new CellRangeAddress(srow, srow + rspan, 1, 1)); 38 oldvalue = sheet.GetRow(r).GetCell(cunit).StringCellValue; 39 srow = r; 40 rspan = 0; 41 } 42 } 43 44 // 對未合併的單元格進行合併 45 if (rspan != 0) 46 { 47 sheet.AddMergedRegion(new CellRangeAddress(srow, srow + rspan, 1, 1)); 48 } 49 50 // 調整合並單元格的樣式 51 for (int c = 0; c < 4; c++) 52 { 53 for (int r = 2; r < sheet.PhysicalNumberOfRows; r++) 54 { 55 if (c == cunit) 56 { 57 sheet.GetRow(r).GetCell(c).CellStyle = mstyle; 58 } 59 else 60 { 61 sheet.GetRow(r).GetCell(c).CellStyle = style; 62 } 63 } 64 } 65 }
導出後的文件內容: