C# NPOI Excel固定模板写入数据

导出数据触发:
<div>

后台实现:

//Excel模板
 string TempletFileName = context.Server.MapPath("..../UploadFile/test.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;
                for (int i = 0; i < exportTable.Rows.Count; i++)
                {
                    IRow row = sheet1.CreateRow(nRow);
                    for (int j = 0; j < exportTable.Columns.Count; j++)
                    {
                    //添加数据到excel中                        row.CreateCell(j).SetCellValue(exportTable.Rows[i][j]);
                    }
                    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());

你可能感兴趣的:(资料记录,excel)