NPOI导出excel。根据模板导出Excel
电脑版发表于:2019/1/27 23:32
使用NPOI导出excel
/// <summary> /// 导出excel(下载excel) /// </summary> public void ToExcel() { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.CreateSheet() as HSSFSheet; HSSFRow row = sheet.CreateRow(0) as HSSFRow; HSSFCell cell = row.CreateCell(0) as HSSFCell; cell.SetCellValue("用户名"); HSSFCellStyle style = workbook.CreateCellStyle() as HSSFCellStyle; style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; //合并单元格 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 0)); sheet.SetColumnWidth(0, 1000); //导出excel Response.AddHeader("Content-Disposition", "attachment; filename=用户.xls"); MemoryStream ms = new MemoryStream(); workbook.Write(ms); Response.BinaryWrite(ms.ToArray()); }
.net core 中导出excel参考:https://www.tnblog.net/aojiancc2/article/details/7787
更多设置可以参考:
导出增加样式
public ActionResult OutPutExcel() { //在内存中构建一个excel HSSFWorkbook workbook = new HSSFWorkbook(); //在创建一个excel的表 HSSFSheet sheet = workbook.CreateSheet("学生信息表") as HSSFSheet; //创建行 HSSFRow headerrow = sheet.CreateRow(0) as HSSFRow; //设置行高 headerrow.Height = 1200; for (int i = 0; i < 3; i++) { sheet.SetColumnWidth(i, 220 * 25); } HSSFCell headcell = headerrow.CreateCell(0) as HSSFCell; headcell.SetCellValue("重庆优质学生信息公示\nQQ:48672901"); //头部样式 HSSFCellStyle headCellStyle = workbook.CreateCellStyle() as HSSFCellStyle; headCellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; headCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; headCellStyle.WrapText = true; //设置字体样式 HSSFFont font = workbook.CreateFont() as HSSFFont; font.Boldweight = 800; font.FontHeight = 350; font.Color = HSSFColor.RED.index; font.FontName = "微软雅黑"; headCellStyle.SetFont(font); headcell.CellStyle = headCellStyle; //创建行 HSSFRow row = sheet.CreateRow(1) as HSSFRow; //创建列(填充数据) row.CreateCell(0).SetCellValue("用户名"); HSSFCell cell = row.CreateCell(1) as HSSFCell; cell.SetCellValue("学号"); row.CreateCell(2).SetCellValue("年龄"); row.CreateCell(3).SetCellValue("班级"); //合并单元格(合并第一行的第1列到第3列) sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 3)); //合并单元格(合并行,合并第一行的第2到4列) //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(2, 4, 0, 0)); //创建一个样式类 HSSFCellStyle cellstyle = workbook.CreateCellStyle() as HSSFCellStyle; //水平居中 cellstyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; //垂直居中 cellstyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; cell.CellStyle = cellstyle; //把内存流做为文件下载中转 MemoryStream memoryStream = new MemoryStream(); workbook.Write(memoryStream); Response.AddHeader("Content-Disposition", "attachment; filename=学生信息表.xls"); Response.BinaryWrite(memoryStream.ToArray()); return View(); }
根据模板导出
public void OutPutExcelByTemp() { string url = Server.MapPath("~/excel/temp.xls"); FileStream filestream = new FileStream(url, FileMode.Open); //根据文件流加载excel HSSFWorkbook workbook = new HSSFWorkbook(filestream); //获取表 HSSFSheet sheet = workbook.GetSheet("2021年度财务报表") as HSSFSheet; for (int i = 3; i < 10; i++) { HSSFRow row = sheet.CreateRow(i) as HSSFRow; row.Height = 700; for (int j = 0; j < 8; j++) { HSSFCell cell = row.CreateCell(j) as HSSFCell; //创建一个样式类 HSSFCellStyle cellstyle = workbook.CreateCellStyle() as HSSFCellStyle; //水平居中 cellstyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; //垂直居中 cellstyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; cell.SetCellValue("富婆" + i * j); cell.CellStyle = cellstyle; } } //把内存流做为文件下载中转 MemoryStream memoryStream = new MemoryStream(); workbook.Write(memoryStream); Response.AddHeader("Content-Disposition", "attachment; filename=重庆富婆通讯录.xls"); Response.BinaryWrite(memoryStream.ToArray()); }