1:EPPlus.core 弃用的情况下(EPPlus.core 不支持net Core3.0)可使用 EPPlus 包 【因文件超10M,在最下面提供百度网盘地址】
2:EPPlus 引用方式
A)工具---->NuGet 包管理器--->管理解决方案的NuGet程序包--->搜索安装
B) 工具---->NuGet 包管理器--->程序包管理器控制台--->Install-Package EPPlus
3:导出
a:工具方法
1 /// <summary> 2 /// 导出 3 /// </summary> 4 /// <param name="ePPlus"></param> 5 /// <returns></returns> 6 public static MemoryStream Export(ICollection<EPPlus> ePPlus) 7 { 8 MemoryStream stream = new MemoryStream(); 9 ExcelPackage package = new ExcelPackage(stream); 10 11 package.Workbook.Worksheets.Add("EPPlus"); 12 ExcelWorksheet sheet = package.Workbook.Worksheets[0]; 13 14 #region write header 15 sheet.Cells[1, 1].Value = "用户名"; 16 sheet.Cells[1, 2].Value = "年龄"; 17 sheet.Cells[1, 3].Value = "性别"; 18 sheet.Cells[1, 4].Value = "成绩"; 19 20 using (ExcelRange range = sheet.Cells[1, 1, 1, 4]) 21 { 22 range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; 23 range.Style.Fill.BackgroundColor.SetColor(Color.Gray); 24 range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thick; 25 range.Style.Border.Bottom.Color.SetColor(Color.Black); 26 range.AutoFitColumns(4); 27 } 28 #endregion 29 30 #region write content 31 int pos = 2; 32 foreach (EPPlus s in ePPlus) 33 { 34 sheet.Cells[pos, 1].Value = s.Name; 35 sheet.Cells[pos, 2].Value = s.Age; 36 sheet.Cells[pos, 3].Value = s.Gender; 37 sheet.Cells[pos, 4].Value = s.Achievement; 38 39 using (ExcelRange range = sheet.Cells[pos, 1, pos, 4]) 40 { 41 range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin; 42 range.Style.Border.Bottom.Color.SetColor(Color.Black); 43 range.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left; 44 } 45 46 pos++; 47 } 48 #endregion 49 50 package.Save(); 51 52 return stream; 53 }