NPOI导出Excel表格

public ActionResult ExportExcel()
        {
            var wk = new XSSFWorkbook();
            string filename = "导出Excel表格测试.xlsx";//Excel表格名称
            try
            {
                    string strFileName = string.Empty;
                    ISheet sheet = wk.CreateSheet("Sheet页名称");//Sheet页名称
                    IRow rowHeader = sheet.CreateRow(0);//表头行
                    rowHeader.CreateCell(0, CellType.String).SetCellValue("批次");
                    rowHeader.CreateCell(1, CellType.String).SetCellValue("志愿序号");
                    rowHeader.CreateCell(2, CellType.String).SetCellValue("院校代号");
                    rowHeader.CreateCell(3, CellType.String).SetCellValue("专业代号");
                    rowHeader.CreateCell(4, CellType.String).SetCellValue("院校名称");
                    rowHeader.CreateCell(5, CellType.String).SetCellValue("专业名称");
                    for (int i = 0; i < 10; i++)
                    {
                        IRow row = sheet.CreateRow(i + 1);
                        row.CreateCell(0, CellType.String).SetCellValue("本科第一批");
                        row.CreateCell(1, CellType.String).SetCellValue(i+1);
                        row.CreateCell(2, CellType.String).SetCellValue("0001");
                        row.CreateCell(3, CellType.String).SetCellValue("10");
                        row.CreateCell(4, CellType.String).SetCellValue("清华大学");
                        row.CreateCell(5, CellType.String).SetCellValue("临床医学");
                        sheet.SetColumnWidth(4, 256 * 40);//设置第五列(院校名称)的宽度
                        sheet.SetColumnWidth(5, 256 * 100);//设置第六列(专业名称)的宽度
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //发送到客户端
            MemoryStream ms = new MemoryStream();
            wk.Write(ms);

            //通知浏览器下载文件而不是打开 
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)));
            Response.BinaryWrite(ms.ToArray());
            Response.Flush();
            Response.End();
            wk = null;
            ms.Close();
            ms.Dispose();
            return null;
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值