diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index d21b8db..5adc237 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java @@ -15,6 +15,7 @@ import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContextImpl; import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisStopException; +import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; @@ -113,40 +114,44 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { if (readWorkbookHolder.getReadCache() != null) { readWorkbookHolder.getReadCache().destroy(); } - } catch (Throwable e) { - throw new ExcelAnalysisException("Can not close IO", e); + } catch (Throwable t) { + throwCanNotCloseIo(t); } try { if (readWorkbookHolder.getOpcPackage() != null) { readWorkbookHolder.getOpcPackage().close(); } - } catch (Throwable e) { - throw new ExcelAnalysisException("Can not close IO", e); + } catch (Throwable t) { + throwCanNotCloseIo(t); } try { if (readWorkbookHolder.getPoifsFileSystem() != null) { readWorkbookHolder.getPoifsFileSystem().close(); } - } catch (Throwable e) { - throw new ExcelAnalysisException("Can not close IO", e); + } catch (Throwable t) { + throwCanNotCloseIo(t); } try { if (analysisContext.readWorkbookHolder().getAutoCloseStream() && readWorkbookHolder.getInputStream() != null) { readWorkbookHolder.getInputStream().close(); } - } catch (Throwable e) { - throw new ExcelAnalysisException("Can not close IO", e); + } catch (Throwable t) { + throwCanNotCloseIo(t); } try { if (readWorkbookHolder.getTempFile() != null) { FileUtils.delete(readWorkbookHolder.getTempFile()); } - } catch (Throwable e) { - throw new ExcelAnalysisException("Can not close IO", e); + } catch (Throwable t) { + throwCanNotCloseIo(t); } } + private void throwCanNotCloseIo(Throwable t) { + throw new ExcelAnalysisException("Can not close IO", t); + } + @Override public com.alibaba.excel.analysis.ExcelExecutor excelExecutor() { return excelExecutor; diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index ebe0d26..6ad7fcb 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -9,6 +9,7 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -343,39 +344,52 @@ public class WriteContextImpl implements WriteContext { if (writeWorkbookHolder == null) { return; } + try { writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream()); writeWorkbookHolder.getWorkbook().close(); - } catch (Throwable e) { - throw new ExcelGenerateException("Can not close IO", e); + } catch (Throwable t) { + throwCanNotCloseIo(t); + } + try { + Workbook workbook = writeWorkbookHolder.getWorkbook(); + if (workbook instanceof SXSSFWorkbook) { + ((SXSSFWorkbook)workbook).dispose(); + } + } catch (Throwable t) { + throwCanNotCloseIo(t); } try { if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) { writeWorkbookHolder.getOutputStream().close(); } - } catch (Throwable e) { - throw new ExcelGenerateException("Can not close IO", e); + } catch (Throwable t) { + throwCanNotCloseIo(t); } try { if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getTemplateInputStream() != null) { writeWorkbookHolder.getTemplateInputStream().close(); } - } catch (Throwable e) { - throw new ExcelGenerateException("Can not close IO", e); + } catch (Throwable t) { + throwCanNotCloseIo(t); } try { if (!writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getFile() != null && writeWorkbookHolder.getOutputStream() != null) { writeWorkbookHolder.getOutputStream().close(); } - } catch (Throwable e) { - throw new ExcelGenerateException("Can not close IO", e); + } catch (Throwable t) { + throwCanNotCloseIo(t); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Finished write."); } } + private void throwCanNotCloseIo(Throwable t) { + throw new ExcelGenerateException("Can not close IO", t); + } + @Override public Sheet getCurrentSheet() { return writeSheetHolder.getSheet(); diff --git a/update.md b/update.md index 7f672d5..f0bf39b 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 2.0.0-beta3 +* 导出完成移除临时目录 [Issue #386](https://github.com/alibaba/easyexcel/issues/386) + # 2.0.0-beta2 * 加速gc回收 [Issue #511](https://github.com/alibaba/easyexcel/issues/511) * 修改空字符串读取可能读取上个字段的数据的bug