.Net Core实现Excel导出

在.Net Core中我们可以使用EPPLUS.Core包来实现Excel文件的操作,它不支持导出2003版的Excel(xls)。

ExcelWorksheet.Cells对象支持多种数据来源加载,包括Text、Arrays、Collection、DataTable、DataReader。

List<user> list = _SerUser.GetListByExport();
int isAdopt = 0;

string webRootFolder = _HostingEnvironment.WebRootPath;
string relativeFolder = $"/export/{DateTime.Now.ToString("yyyy")}";
string absoluteFolder = $"{webRootFolder}/{relativeFolder}";

if (!Directory.Exists(absoluteFolder))
{
    Directory.CreateDirectory(absoluteFolder);
}

string fileName = $"{Guid.NewGuid()}.xlsx";
string relativeFilePath = $"{relativeFolder}/{fileName}";
string absoluteFilePath = $"{absoluteFolder}/{fileName}";

try
{
    FileInfo file = new FileInfo(Path.Combine(webRootFolder, absoluteFilePath));
    using (ExcelPackage package = new ExcelPackage(file))
    {
        ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("sheetname");
        worksheet.Cells.LoadFromCollection(list, true);
        package.Save();
        isAdopt = 1;
    }
    return new ApiResult(isAdopt, "导出成功", relativeFilePath);
}
catch
{
    isAdopt = 0;
    return new ApiResult(isAdopt, "导出失败");
}
上一条记录:Vue.js如何集成使用蚂蚁数据可视化G2
下一条记录:-

添加新评论取消回复

昵称:
验证码: 验证码,看不清楚?请点击刷新验证码
评论: