摘要:使用开源程序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 }
导出后的文件内容: