.net core webapi 导出excel
nuget安装 EPPlus.Core .
使用起来也很简单,我们构造 ExcelHelper 类,并在controller里面使用。
比如 person类有 id,name,age 3个属性,则 在controller里面这样调用
[Route("ExportExcel")]
[HttpGet]
public IActionResult ExportExcel(){
var personList=new List<Person>(){
new Person(){id=1,name="wufan",age=25},
new Person(){id=2,name="you",age=26}
}
var heads=new List<string>() { "编号", "姓名", "年龄"};
var excelFilePath = ExcelHelper.CreateExcelFromList(personList,heads)
return File(
new FileStream(excelFilePath, FileMode.Open),
"application/octet-stream",
"ExcelNameHere.xlsx"
);
}
下面是 ExcelHelper参考代码
using OfficeOpenXml;
public class ExcelHelper
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataList">数据</param>
/// <p