Skip to content

Commit

Permalink
导出完成移除临时目录 #386
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Sep 2, 2019
1 parent 773109c commit 65f2243
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
25 changes: 15 additions & 10 deletions src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -113,40 +114,44 @@ public void finish() {
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;
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/com/alibaba/excel/context/WriteContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;

Expand Down Expand Up @@ -343,39 +344,52 @@ public void finish() {
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();
Expand Down
3 changes: 3 additions & 0 deletions update.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 65f2243

Please sign in to comment.