.NET NPOI 导出Excel
1.前端Javascript 构建Form表单
FormPost: function (URL, PARAMS) { var me = this; var temp = document.createElement("form"); temp.action = URL; temp.id = "form1"; temp.name = "form1"; temp.method = "post"; temp.style.display = "none"; // 创建一个输入 var input = document.createElement("input"); // 设置相应参数 input.type = "text"; input.name = "Method"; input.value = "ExportExcel"; // 将该输入框插入到 form 中 temp.appendChild(input); document.body.appendChild(temp); temp.submit();
document.body.removeChild(temp); },
2.后台C# 代码
/// <summary> /// 导出Excel /// </summary> /// <param name="context"></param> /// <returns></returns> public void ExportExcel(HttpContext context) { List<string> headList = new List<string>(); headList.Add("语文"); headList.Add("数学"); headList.Add("英语"); headList.Add("政治"); List<string> nameList = new List<string>(); nameList.Add("张三"); nameList.Add("李四"); nameList.Add("王五"); nameList.Add("赵六"); //创建工作簿 HSSFWorkbook book = new HSSFWorkbook(); //增加标题Style HSSFCellStyle styleHeader = (HSSFCellStyle)book.CreateCellStyle(); styleHeader.FillForegroundColor = HSSFColor.Blue.Index; styleHeader.FillPattern = FillPattern.SolidForeground; //设置Font HSSFFont fontHeader = (HSSFFont)book.CreateFont(); fontHeader.FontName = "Tahoma"; fontHeader.FontHeight = 200; fontHeader.Color = HSSFColor.White.Index; styleHeader.SetFont(fontHeader); //设置缺省Style HSSFCellStyle styleDefault = (HSSFCellStyle)book.CreateCellStyle(); HSSFFont fontDefault = (HSSFFont)book.CreateFont(); styleDefault.SetFont(fontDefault); fontDefault.FontName = "Tahoma"; fontDefault.FontHeight = 200; //创建Sheet HSSFRow row; HSSFCell cell; HSSFSheet sheet = (HSSFSheet)book.CreateSheet("Sheet1"); //创建标题列 row = (HSSFRow)sheet.CreateRow(0); for (int i = 0; i < headList.Count; i++) { //创建cell cell = (HSSFCell)row.CreateCell(i, CellType.String); //应用style HSSFCellStyle style = (HSSFCellStyle)book.CreateCellStyle(); style.CloneStyleFrom(styleHeader); style.Alignment = HorizontalAlignment.Center; cell.CellStyle = style; //设置值 cell.SetCellValue(headList[i]);
//设置单元格宽度
sheet.SetColumnWidth(i, 30*265);
} for (int i = 0; i < nameList.Count; i++) { row = (HSSFRow)sheet.CreateRow(sheet.LastRowNum + 1); cell = (HSSFCell)row.CreateCell(i, CellType.String); cell.SetCellValue(nameList[i]); cell.CellStyle = (HSSFCellStyle)book.CreateCellStyle(); cell.CellStyle.CloneStyleFrom(styleDefault); cell.CellStyle.Alignment = HorizontalAlignment.Center; } byte[] bytes; using (MemoryStream ms = new MemoryStream()) { book.Write(ms); bytes = ms.ToArray(); } //导出文件名 /* string fileName = context.Request.Params["fileName"] == null?"123":"456" */; string fileName = ""; if (String.IsNullOrEmpty(fileName)) fileName = "Export"; fileName += YZStringHelper.DateToString(DateTime.Now) + ".xls"; this.ResponseHeader(context, fileName, true); context.Response.BinaryWrite(bytes); }
//构建响应头 protected void ResponseHeader(HttpContext context, string fileName, bool attachment) { string fileExt = Path.GetExtension(fileName); string contentType = YZMimeMapping.GetMimeType(fileExt); fileName = context.Server.UrlEncode(fileName); fileName = fileName.Replace("%5b", "["); fileName = fileName.Replace("%5d", "]"); context.Response.AppendHeader("Content-Type", contentType); if (attachment) context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); else context.Response.AppendHeader("Content-Disposition", "filename=" + fileName); context.Response.ContentEncoding = Encoding.UTF8; context.Response.Buffer = true; context.Response.Charset = Encoding.UTF8.BodyName; context.Response.AppendHeader("Accept-Ranges", "bytes"); }