本文导出Excel表格是基于NPOI
NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作。
NPOI的下载、引用 下载地址https://github.com/KezhanW/NPOI 然后我们打开解压好的文件,看到有dotnet2和dotnet4文件夹,这里我们打开dotnet4,引入全部的dll 接下来using
1 2 3 4 5 6 7 using NPOI;using NPOI.SS.UserModel;using NPOI.HSSF.UserModel;using NPOI.XSSF.UserModel;using NPOI.HSSF.Util;using NPOI.SS.Util;
下面就可以开始我们的项目了
导出Excel表格
注:可以添加单元格样式、合并单元格等操作 如无需删除即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 public JsonResult ExportExcel (string ids ) { IWorkbook workbook = new XSSFWorkbook(); workbook.CreateSheet("sheet1" ); ISheet sheet1 = workbook.GetSheet("sheet1" ); sheet1.SetColumnWidth(0 , 256 * 20 ); sheet1.SetColumnWidth(1 , 256 * 20 ); sheet1.SetColumnWidth(2 , 256 * 20 ); sheet1.SetColumnWidth(3 , 256 * 20 ); sheet1.SetColumnWidth(4 , 256 * 20 ); sheet1.SetColumnWidth(5 , 256 * 20 ); sheet1.SetColumnWidth(6 , 256 * 20 ); ICellStyle style = workbook.CreateCellStyle(); style.BorderBottom = BorderStyle.Thin; style.BorderTop = BorderStyle.Thin; style.BorderLeft = BorderStyle.Thin; style.BorderRight = BorderStyle.Thin; style.BottomBorderColor = HSSFColor.Black.Index; style.LeftBorderColor = HSSFColor.Black.Index; style.RightBorderColor = HSSFColor.Black.Index; style.TopBorderColor = HSSFColor.Black.Index; style.Alignment = HorizontalAlignment.CenterSelection; KCMS_EF kef = new KCMS_EF(); List<SysAdmin> userLogin = kef.SysAdmin.Where(p => p.IsLogin == 1 ).ToList(); IRow row = sheet1.CreateRow(0 ); row.HeightInPoints = 16 ; row.CreateCell(0 ).SetCellValue("Excel的导出" ); row.GetCell(0 ).CellStyle = style; IRow row1 = sheet1.CreateRow(1 ); row1.HeightInPoints = 16 ; row1.CreateCell(0 ).SetCellValue("账号" ); row1.GetCell(0 ).CellStyle = style; row1.CreateCell(1 ).SetCellValue("姓名" ); row1.GetCell(1 ).CellStyle = style; row1.CreateCell(2 ).SetCellValue("邮箱" ); row1.GetCell(2 ).CellStyle = style; var i = 2 ; foreach (var item in userLogin) { IRow row2 = sheet1.CreateRow(i); row2.HeightInPoints = 16 ; row2.CreateCell(0 ).SetCellValue(item.LoginName); row2.GetCell(0 ).CellStyle = style; row2.CreateCell(1 ).SetCellValue(item.RealName); row2.GetCell(1 ).CellStyle = style; row2.CreateCell(2 ).SetCellValue(item.Email); row2.GetCell(2 ).CellStyle = style; i++; } CellRangeAddress region = new CellRangeAddress(0 , 0 , 0 , 2 ); sheet1.AddMergedRegion(region); sheet1.ForceFormulaRecalculation = true ; string str = "Task List-" + DateTime.Now.ToString("yyyyMMddHHmmss" ) + ".xlsx" ; DateTime now = DateTime.Now; string text = string .Concat(new string [] { "/UpFiles/ExcelFiles/" , now.Year.ToString(), "/" , now.Month.ToString(), "/" , now.Day.ToString(), "/" }); string text2 = Server.MapPath(text); if (!Directory.Exists(text2)) { Directory.CreateDirectory(text2); } byte [] buffer; MemoryStream ms = new MemoryStream(); workbook.Write(ms); buffer = ms.ToArray(); System.IO.File.WriteAllBytes(text2 + str, buffer); return Json(new { code = 200 , msg = text + str }); }
前台请求方法及下载
1 2 3 4 5 6 7 8 $.post("/practice/ExportExcel" , {}, function (data ) { var a = document .createElement('a' ); var filename = '分票数据导出.xlsx' ; a.href = data.msg; a.download = filename; a.click(); })
插入行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void ShiftRows (int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight ) ;例:sheet.ShiftRows(insertRow,sheet.LastRowNum,1 ,true ,false ); sheet.CreateRow(insertRow);
添加单元格背景色 1 2 3 4 5 6 ICellStyle style = workbook.CreateCellStyle(); style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index; style.FillPattern = FillPattern.SolidForeground; ICell cell = workbook.CreateSheet().CreateRow(0 ).CreateCell(0 ); cell.CellStyle = style;
修改字体 1 2 3 4 5 6 7 8 9 ICellStyle style = workbook.CreateCellStyle(); IFont font = workbook.CreateFont(); font.IsBold = true ; font.FontName = "Microsoft YaHei" ; font.Color = HSSFColor.LightOrange.Index; style.SetFont(font); ICell cell = workbook.CreateSheet().CreateRow(0 ).CreateCell(0 ); cell.CellStyle = style;
组合行 1 2 sheet.GroupRow(0 , 10 ); sheet.SetRowGroupCollapsed(0 , true )
导出表格样式