From 65f22432189d9b75741b142bfb23045ab4e7bb24 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Mon, 2 Sep 2019 16:51:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=AE=8C=E6=88=90=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E4=B8=B4=E6=97=B6=E7=9B=AE=E5=BD=95=20#386?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/analysis/ExcelAnalyserImpl.java | 25 +++++++++------- .../excel/context/WriteContextImpl.java | 30 ++++++++++++++----- update.md | 3 ++ 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index d21b8db0..5adc237e 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 ebe0d269..6ad7fcbb 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 7f672d54..f0bf39bb 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