0%

Abp 文件导出

Abp文件导出, 可以在分布式服务层导出(web.core层), 但是如果是列表数据导出, 若在web.core层写的话, 这样我们到导出功能与应用层分开了, 这样对于后期维护, 这会非常烦恼, 所以今天就把导出的功能写到应用层中

封装导出方法

  1. FileContentResult返回文件
    ExcelManager中定义GetFileResponse静态文件获取方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public static FileContentResult GetFileResponse<T>(List<T> data, string strFileName, string sheetName)
    {
    try
    {
    FileContentResult fileContentResult = new FileContentResult(ListToDownExcel(data, sheetName).ToArray(), "application/vnd.ms-excel")
    {
    FileDownloadName = strFileName
    };
    return fileContentResult;

    }
    catch (Exception)
    {
    throw new UserFriendlyException("服务器繁忙,请重试!");
    }

    }

    其中ListToDownExcel的方法主要是获取文件流, 需要根据具体业务

  2. Application层中应用导出接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public async Task<FileContentResult> ExportAsset(AssetSearchPageInput input)
    {
    //获取需要导出的数据
    var dataList = await _assetManager.GetLoadFilterData(input.Asset).ToListAsync();
    //转换为导出模型
    var datas = ObjectMapper.Map<List<AssetExportDto>>(dataList);
    //execl文件导出
    return ExcelManager.GetFileResponse(datas, "xxx.xls", "xxx");
    }