爱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日

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境