2 回答
TA贡献1802条经验 获得超5个赞
您可以将工作簿的名称存储在列表中,而不是存储工作簿本身
private List<String> workbooks = new ArrayList();
重写 createWorkbook 以仅存储 excel 工作表的名称 重写 write 方法,使其创建一个新工作簿,如下所示
public void write(List<ExcelData> data, String workbookName) {
Workbook workbook = new XSSFWorkbook();
CreationHelper createHelper = workbook.getCreationHelper();
... 写东西
FileOutputStream fileOut = new FileOutputStream(workbookName + ".xlsx");
workbook.write(fileOut);
fileOut.close();
workbook.close();
}
TA贡献1783条经验 获得超4个赞
这个错误主要发生在我们试图写入已经关闭的工作簿的内容时。
public void writeToExcel(File file) throws IOException {
LOGGER.debug("Writing chunks data to excel {}", file);
try (FileOutputStream outputStream = new FileOutputStream(file)) {
workbook.write(outputStream);
} catch (IOException e) {
LOGGER.error("Exception raised while writing chunks of items {}", e.getLocalizedMessage());
} finally {
// This line will take of closing XSSFWorkbook along with SXSSFWorkbook
workbook.close();
}
}
这段代码是抛出以下异常的代码
Exception thrown is:
Exception raised while writing chunks of items
"Cannot write data, document seems to have been closed already"
为什么我们得到这个例外?看看这段代码
private void instructionSheet(Resource resource) {
try {
InputStream in = resource.getInputStream();
// This is try-with-resources block which is closing the XSSFWorkbook
try (XSSFWorkbook xssfwb = new XSSFWorkbook(OPCPackage.open(in))) {
workbook = new SXSSFWorkbook(xssfwb);
}
} catch (IOException | InvalidFormatException e) {
LOGGER.error("The instruction sheet failed to create {}", e.getLocalizedMessage());
}
}
您可以注意到第二个 try 块是 try-with-resources 块并且正在关闭工作簿,因此我们得到了异常。
我们只是通过删除第二个 try 块来解决它,即
private void instructionSheet(Resource resource) {
try {
workbook = new SXSSFWorkbook(new XSSFWorkbook(OPCPackage.open(resource.getInputStream())));
} catch (IOException | InvalidFormatException e) {
LOGGER.error("The instruction sheet failed to create {}", e.getLocalizedMessage());
}
}
您可以在这个答案的第一个代码块中注意到,我们在将内容写入文件后关闭工作簿。
在 finally 块中调用的close方法将负责关闭XSSFWorkbook实例以及SXSSFWorkbook。
添加回答
举报