NPOI方式导出Excel

NPOI官方网址:http://tonyqus.sinaapp.com/。需要首选引用:NPOI.dll。

对于我们开发者使用的对象主要位于NPOI.HSSF.UserModel空间下,主要有HSSFWorkbookHSSFSheetHSSFRowHSSFCell,对应的接口为位于NPOI.SS.UserModel空间下的IWorkbookISheetIRowICell,分别对应Excel文件、工作表、行、列。

一、将DataTable类型数据读取到流中

   public MemoryStream RenderToExcel(DataTable table)
        {
            MemoryStream ms = new MemoryStream();

            using (table)
            {
                using (IWorkbook workbook = new HSSFWorkbook())
                {
                    //创建字体样式
                    IFont headFont = workbook.CreateFont();
                    headFont.Boldweight = (short)FontBoldWeight.BOLD;
                    //创建单元格样式
                    ICellStyle headCellStyle = workbook.CreateCellStyle();
                    headCellStyle.Alignment = HorizontalAlignment.CENTER;
                    headCellStyle.VerticalAlignment = VerticalAlignment.CENTER;
                    headCellStyle.SetFont(headFont);

                    using (ISheet sheet = workbook.CreateSheet())
                    {
                        //列头
                        IRow headerRow = sheet.CreateRow(0);
                        headerRow.CreateCell(0).SetCellValue("列1");
                        headerRow.CreateCell(1).SetCellValue("列2");
                        headerRow.CreateCell(2).SetCellValue("列3");
                        headerRow.CreateCell(3).SetCellValue("列4");
                        headerRow.CreateCell(4).SetCellValue("列5");
                        headerRow.CreateCell(5).SetCellValue("列6");
                       //将列头单元格字体加粗
                        foreach (ICell headerCell in headerRow.Cells)
                        {
                            headerCell.CellStyle = headCellStyle;
                        }

                        //设置行高
                        headerRow.Height = 20 * 20;

                        //设置列宽
                        sheet.SetColumnWidth(0, 6 * 256);
                        sheet.SetColumnWidth(1, 20 * 256);
                        sheet.SetColumnWidth(2, 18 * 256);
                        sheet.SetColumnWidth(3, 15 * 256);
                        sheet.SetColumnWidth(4, 20 * 256);
                        sheet.SetColumnWidth(5, 20 * 256);
                        //列表
                        int rowIndex = 1;
                        foreach (DataRow row in table.Rows)
                        {
                            IRow dataRow = sheet.CreateRow(rowIndex);
                            dataRow.CreateCell(0).SetCellValue(row["a"].ToString());
                            dataRow.CreateCell(1).SetCellValue(row["b"].ToString());
                            dataRow.CreateCell(2).SetCellValue(row["c"].ToString());
                            dataRow.CreateCell(3).SetCellValue(row["d"].ToString());
                            dataRow.CreateCell(4).SetCellValue(row["e"].ToString());
                            dataRow.CreateCell(5).SetCellValue(row["f"].ToString());
                            
                            rowIndex++;
                        }

                        workbook.Write(ms);
                        ms.Flush();
                        ms.Position = 0;
                    }
                }
            }
            return ms;
        }

二、将流中内容保存在硬盘,或者通过浏览器下载:

以上代码把创建的Workbook对象保存到流中,可以通过以下方法输出到浏览器:

       string fileName = "导出Excel.xls";
            if (Request.Browser.Browser == "IE")
            {
                fileName = HttpUtility.UrlEncode(fileName);
            }

            Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
       Response.BinaryWrite(ms.ToArray());

 或者存硬盘中:

    using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
      {
          byte[] data = ms.ToArray();
          fs.Write(data, 0, data.Length);
          fs.Flush();
          data = null;
      }
posted @ 2013-03-28 16:33  jinjiabo  阅读(205)  评论(0编辑  收藏  举报