.net core webapi 导出excel

.NET Core WebAPI中通过安装EPPlus.Core库实现Excel导出,简化了处理过程。在Controller中调用ExcelHelper方法,如针对包含id、name、age属性的person类。此外,还介绍了如何结合Vue和axios实现在前端下载生成的Excel文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值