爱Java的球迷 2023-09-20 16:12 采纳率: 0%
浏览 2

导出多个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的导出功能。

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-09-20 16:20
    关注

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

    评论

报告相同问题?

问题事件

  • 创建了问题 9月20日

悬赏问题

  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥50 如何将脑的图像投影到颅骨上
  • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境
  • ¥15 netcore使用PuppeteerSharp截图
  • ¥20 这张图页脚具体代码该怎么写?
  • ¥15 关于#sql#的问题,请各位专家解答!
  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥15 allegro17.2生成bom表是空白的