C#使用NPOI读取excel模板,并导出excel

        private void ExportDoctoryCase(HttpContext context)
        {
            //加载模板文件路径  
            string TempletFileName = context.Server.MapPath("../ExcelTemplate/Template.xls");
            HSSFWorkbook wk = null;
            using (FileStream fs = File.Open(TempletFileName, FileMode.Open,
            FileAccess.Read, FileShare.ReadWrite))
            {
                //把xls文件读入workbook变量里,之后就可以关闭了
                wk = new HSSFWorkbook(fs);
                fs.Close();
            }
            HSSFSheet sheet1 = (HSSFSheet)wk.GetSheetAt(0);
            DoctorBLL bll = new DoctorBLL();
            DataTable exportTable = bll.GetExportQuestionTable();
            if (exportTable != null)
            {
                int nRow = 2;//开始插入的行(第三行)
                string nextFirstTxt = string.Empty;
                ICellStyle style1 = SetCellStyle(wk, null, HorizontalAlignment.CENTER);
                for (int i = 0; i < exportTable.Rows.Count; i++)
                {
                    IRow row = sheet1.CreateRow(nRow);//创建行
                    for (int j = 0; j < exportTable.Columns.Count; j++)
                    {
                      //创建列并赋值
                      row.CreateCell(j).SetCellValue(exportTable.Rows[i][j].ToString());
                    }
                    //设置Excel行的样式(带边框)
                    row.GetCell(j).CellStyle = style1;
                    nRow++;
                }
            }
            context.Response.ContentType = "application/vnd.ms-excel";
            context.Response.AddHeader("Content-Disposition", "attachment;filename=问卷导出" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            context.Response.AddHeader("Content-Transfer-Encoding", "binary");
            context.Response.ContentType = "application/octet-stream";
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            MemoryStream file = new MemoryStream();
            wk.Write(file);
            context.Response.BinaryWrite(file.GetBuffer());
        }


       /// <summary>
       /// 给Excel添加边框
       /// </summary>
       public static ICellStyle SetCellStyle(HSSFWorkbook hssfworkbook, IFont font, HorizontalAlignment ha)
        {
            ICellStyle cellstyle = hssfworkbook.CreateCellStyle();
            cellstyle.Alignment = ha;
            if (font != null)
            {
                cellstyle.SetFont(font);
            }
            //有边框
            cellstyle.BorderBottom = BorderStyle.THIN;
            cellstyle.BorderLeft = BorderStyle.THIN;
            cellstyle.BorderRight = BorderStyle.THIN;
            cellstyle.BorderTop = BorderStyle.THIN;
            return cellstyle;
        }
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值