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,
有哥们知道是什么原因吗?
我的需求是传入多个logId,导出每个logId对应的Excel数据信息。就有多个Excel文件导出,现在的代码只能导出一个。求指导。我的是Springboot项目,若依自带的EasyExcel的导出功能。