导出多个Excel文件出错


 public void exportData(@RequestBody List<Long> logIds, HttpServletResponse response, HttpServletRequest request) {
        List<String> keys = Arrays.asList("acctMonth", "areaId", "areaName", "incomeSource", "sourceName",
                "horCode", "horName", "indexCode", "indexName", "contractId", "contractName", "indexValue",
                "taxValue", "ictCode", "hkont", "itemCode", "custCode");
        List<String> name = Arrays.asList("acctMonth", "areaId", "areaName", "incomeSource", "sourceName",
                "horCode", "horName", "indexCode", "indexName", "contractId", "contractName", "indexValue",
                "taxValue", "ictCode", "hkont", "itemCode", "custCode");
        //表头信息
        List<List<String>> heads = new ArrayList<>();
        List<String> head0 = null;
        for (String vo : name) {
            head0 = new ArrayList<>();
            head0.add(vo);
            heads.add(head0);
        }
        String fileName = "数据导出.xlsx";
        List<List<RptImportDataC4>> datas = incomeFillingC4EnterpriseService.exportData(logIds);
        //批量导出
        OutputStream outputStream = null;
        ExcelWriter excelWriter = null;
        for (List<RptImportDataC4> rptImportDataC4List : datas) {
            try {
                outputStream = ExportExcelUtil.getOutputStream(fileName, response, request);
                excelWriter = EasyExcel.write(outputStream).build();
                WriteSheet writeSheet = EasyExcel.writerSheet(0, "数据导出").head(heads)
                        .registerWriteHandler(new SimpleColumnWidthStyleStrategy(16))
                        .build();
                List<Map<String, Object>> list = rptImportDataC4List.stream()
                        .map(rptImportData -> {
                            Map<String, Object> resultMap = new HashMap<>();
                            resultMap.put("acctMonth", rptImportData.getAcctMonth());
                            resultMap.put("areaId", rptImportData.getAreaId());
                            resultMap.put("areaName", rptImportData.getAreaName());
                            resultMap.put("incomeSource", rptImportData.getIncomeSource());
                            resultMap.put("sourceName", rptImportData.getSourceName());
                            resultMap.put("horCode", rptImportData.getHorCode());
                            resultMap.put("horName", rptImportData.getHorName());
                            resultMap.put("indexCode", rptImportData.getIndexCode());
                            resultMap.put("indexName", rptImportData.getIndexName());
                            resultMap.put("contractId", rptImportData.getContractId());
                            resultMap.put("contractName", rptImportData.getContractName());
                            resultMap.put("indexValue", rptImportData.getIndexValue());
                            resultMap.put("taxValue", rptImportData.getTaxValue());
                            resultMap.put("ictCode", rptImportData.getIctCode());
                            resultMap.put("hkont", rptImportData.getHkont());
                            resultMap.put("itemCode", rptImportData.getItemCode());
                            resultMap.put("custCode", rptImportData.getCustCode());
                            return resultMap;
                        }).collect(Collectors.toList());
                List<List<String>> dataList = new ArrayList<>();
                if (rptImportDataC4List.size() == 0) {
                    list = new ArrayList<>();
                    Map<String, Object> resultMap = new HashMap<>();
                    resultMap.put("acctMonth", "无数据");
                    list.add(resultMap);
                    dataList = new ArrayList<>();
                    ExportExcelUtil.processData1(keys, list, dataList);
                    excelWriter.write(dataList, writeSheet);
                } else {
                    dataList = new ArrayList<>();
                    ExportExcelUtil.processData1(keys, list, dataList);
                    excelWriter.write(dataList, writeSheet);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new CustomException("数据导出异常");
            } finally {
                try {
                    //刷新流
                    if (excelWriter != null) {
                        excelWriter.finish();
                    }
                    if (outputStream != null) {
                        outputStream.flush();
                        outputStream.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new BaseException(e.getMessage());
                }
            }
        }
    }

如果我传多个LogId的话,只会导出第一个Excel文件,后面就会在excelWriter.finish();这里报错Can not close IO,
有哥们知道是什么原因吗?

img

我的需求是传入多个logId,导出每个logId对应的Excel数据信息。就有多个Excel文件导出,现在的代码只能导出一个。求指导。我的是Springboot项目,若依自带的EasyExcel的导出功能。

不能这么导
可以先多个文件存入服务器目录
打包成zip文件,再发给客户端

【以下回答由 GPT 生成】

解决方案

根据代码段提供的信息,可以看出问题的核心是导出多个Excel文件出错。下面是解决该问题的步骤:

  1. 导入所需的包

在代码段的开头处,需要导入Excel相关的包,以便在代码中使用相应的类和方法。

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
  1. 优化导出逻辑

2.1 修改方法签名

将方法签名修改为:

public void exportData(@RequestBody List<Long> logIds, HttpServletResponse response)

因为不再需要HttpServletRequest对象,在优化后的代码中不再使用。

2.2 优化导出逻辑

public void exportData(@RequestBody List<Long> logIds, HttpServletResponse response) {
    List<String> keys = Arrays.asList("acctMonth", "areaId", "areaName", "incomeSource", "sourceName",
        "horCode", "horName", "indexCode", "indexName", "contractId", "contractName", "indexValue", "taxValue",
        "ictCode", "hkont", "itemCode", "custCode");
    List<String> name = Arrays.asList("acctMonth", "areaId", "areaName", "incomeSource", "sourceName",
        "horCode", "horName", "indexCode", "indexName", "contractId", "contractName", "indexValue", "taxValue",
        "ictCode", "hkont", "itemCode", "custCode");

    String fileName = "数据导出.xlsx";
    List<List<RptImportDataC4>> datas = incomeFillingC4EnterpriseService.exportData(logIds);

    try (OutputStream outputStream = ExportExcelUtil.getOutputStream(fileName, response)) {
        ExcelWriter excelWriter = EasyExcel.write(outputStream).build();

        for (List<RptImportDataC4> rptImportDataC4List : datas) {
            WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
            excelWriter.write(rptImportDataC4List, writeSheet);
        }

        excelWriter.finish();
    } catch (IOException e) {
        // 处理异常
    }
}

以上是对代码进行的优化,主要变动如下:

  • 移除了对HttpServletRequest对象的依赖,因为在代码中没有使用到。
  • 优化了导出逻辑,避免重复创建ExcelWriter对象和WriteSheet对象,减少资源消耗。
  • 使用try-with-resources语句进行流的自动关闭,确保资源正确释放。

请注意,在以上代码片段中,创建OutputStream对象的方法ExportExcelUtil.getOutputStream()和incomeFillingC4EnterpriseService.exportData()是未提供的,您需要根据实际情况进行实现。

如果您提供了这两个方法,我们可以继续优化和完善导出逻辑。如果没有提供这些方法,我无法提供更具体的解决方案,因为我不知道这些方法的实现细节。

补充:如果您在实现这些方法时遇到了具体问题,可以提供更多上下文和详细说明,我将尽力给予帮助。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^