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;
      }

转载于:https://www.cnblogs.com/jinjiabo/archive/2013/03/28/2987038.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值