報表導出時報異常:
org.apache.poi,openxml4j.exceptions.openxml4jruntimeexception:
fail to save:an error occurs while saving the package:the part /docProps/core.xml fail to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller@79f7dd2
報表對象XSSFWorkbook生成以後,需要單獨處理最後一行:獲取最後一個表單的最後一行。
@GetMapping("/exportExcel")
public ObjectRestResponse<PageInfo> exportExcel(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
OutputStream outputStream = null;
try {
XSSFWorkbook xssfWorkbook = profitService.export(toExportList, new BaseEntity());
// /**合併單元格:最後一個sheet最後一行(合計欄),需要合併單元格**/
//XSSFSheet lastSheet = xssfWorkbook.getSheetAt(toExportList.size() - 1); //①
//int lastRowNum = lastSheet.getLastRowNum(); //②
//Row lastRow = lastSheet.getRow(lastRowNum);//③
//lastRow.getCell(0).setCellValue("合計:" + all.getPageInfo().getTotal() + "單");//④
outputStream = response.getOutputStream();
xssfWorkbook.write(outputStream); // 如果通過以①-④修改最後一行,此處報異常
return new ObjectRestResponse<>().rel(true).data(null);
} catch (Exception e) {
log.error(e.getMessage(), e);
ObjectRestResponse result = new ObjectRestResponse<>().rel(false);
result.setRel(false);
result.setStatus(CommonConstants.EX_OTHER_CODE);
result.setMessage("系統繁忙");
return result;
} finally {
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
後來,把上面的代碼①-④去掉,修改最後一行單元格放在profitService.export(toExportList, new BaseEntity()); 處理就沒錯。
public XSSFWorkbook export(List<List<ProfitVO>> data, BaseEntity baseEntity) {
XSSFWorkbook xssfWorkbook = excelExportClient.export(data, new BaseEntity(), "XX報表");
/**合併單元格:最後一個sheet最後一行(合計欄),需要合併單元格**/
XSSFSheet lastSheet = xssfWorkbook.getSheetAt(data.size() - 1);
int lastRowNum = lastSheet.getLastRowNum();
Row lastRow = lastSheet.getRow(lastRowNum);
lastRow.getCell(0).setCellValue("合併");
return xssfWorkbook;
}
原因尚不明確。