Browse Source

导出完成移除临时目录 #386

developing
Jiaju Zhuang 5 years ago
parent
commit
65f2243218
  1. 25
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  2. 30
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  3. 3
      update.md

25
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.context.AnalysisContextImpl;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelAnalysisStopException; 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.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
@ -113,38 +114,42 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
if (readWorkbookHolder.getReadCache() != null) { if (readWorkbookHolder.getReadCache() != null) {
readWorkbookHolder.getReadCache().destroy(); readWorkbookHolder.getReadCache().destroy();
} }
} catch (Throwable e) { } catch (Throwable t) {
throw new ExcelAnalysisException("Can not close IO", e); throwCanNotCloseIo(t);
} }
try { try {
if (readWorkbookHolder.getOpcPackage() != null) { if (readWorkbookHolder.getOpcPackage() != null) {
readWorkbookHolder.getOpcPackage().close(); readWorkbookHolder.getOpcPackage().close();
} }
} catch (Throwable e) { } catch (Throwable t) {
throw new ExcelAnalysisException("Can not close IO", e); throwCanNotCloseIo(t);
} }
try { try {
if (readWorkbookHolder.getPoifsFileSystem() != null) { if (readWorkbookHolder.getPoifsFileSystem() != null) {
readWorkbookHolder.getPoifsFileSystem().close(); readWorkbookHolder.getPoifsFileSystem().close();
} }
} catch (Throwable e) { } catch (Throwable t) {
throw new ExcelAnalysisException("Can not close IO", e); throwCanNotCloseIo(t);
} }
try { try {
if (analysisContext.readWorkbookHolder().getAutoCloseStream() if (analysisContext.readWorkbookHolder().getAutoCloseStream()
&& readWorkbookHolder.getInputStream() != null) { && readWorkbookHolder.getInputStream() != null) {
readWorkbookHolder.getInputStream().close(); readWorkbookHolder.getInputStream().close();
} }
} catch (Throwable e) { } catch (Throwable t) {
throw new ExcelAnalysisException("Can not close IO", e); throwCanNotCloseIo(t);
} }
try { try {
if (readWorkbookHolder.getTempFile() != null) { if (readWorkbookHolder.getTempFile() != null) {
FileUtils.delete(readWorkbookHolder.getTempFile()); FileUtils.delete(readWorkbookHolder.getTempFile());
} }
} catch (Throwable e) { } catch (Throwable t) {
throw new ExcelAnalysisException("Can not close IO", e); throwCanNotCloseIo(t);
}
} }
private void throwCanNotCloseIo(Throwable t) {
throw new ExcelAnalysisException("Can not close IO", t);
} }
@Override @Override

30
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.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -343,39 +344,52 @@ public class WriteContextImpl implements WriteContext {
if (writeWorkbookHolder == null) { if (writeWorkbookHolder == null) {
return; return;
} }
try { try {
writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream()); writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream());
writeWorkbookHolder.getWorkbook().close(); writeWorkbookHolder.getWorkbook().close();
} catch (Throwable e) { } catch (Throwable t) {
throw new ExcelGenerateException("Can not close IO", e); throwCanNotCloseIo(t);
}
try {
Workbook workbook = writeWorkbookHolder.getWorkbook();
if (workbook instanceof SXSSFWorkbook) {
((SXSSFWorkbook)workbook).dispose();
}
} catch (Throwable t) {
throwCanNotCloseIo(t);
} }
try { try {
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) { if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) {
writeWorkbookHolder.getOutputStream().close(); writeWorkbookHolder.getOutputStream().close();
} }
} catch (Throwable e) { } catch (Throwable t) {
throw new ExcelGenerateException("Can not close IO", e); throwCanNotCloseIo(t);
} }
try { try {
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getTemplateInputStream() != null) { if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getTemplateInputStream() != null) {
writeWorkbookHolder.getTemplateInputStream().close(); writeWorkbookHolder.getTemplateInputStream().close();
} }
} catch (Throwable e) { } catch (Throwable t) {
throw new ExcelGenerateException("Can not close IO", e); throwCanNotCloseIo(t);
} }
try { try {
if (!writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getFile() != null if (!writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getFile() != null
&& writeWorkbookHolder.getOutputStream() != null) { && writeWorkbookHolder.getOutputStream() != null) {
writeWorkbookHolder.getOutputStream().close(); writeWorkbookHolder.getOutputStream().close();
} }
} catch (Throwable e) { } catch (Throwable t) {
throw new ExcelGenerateException("Can not close IO", e); throwCanNotCloseIo(t);
} }
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Finished write."); LOGGER.debug("Finished write.");
} }
} }
private void throwCanNotCloseIo(Throwable t) {
throw new ExcelGenerateException("Can not close IO", t);
}
@Override @Override
public Sheet getCurrentSheet() { public Sheet getCurrentSheet() {
return writeSheetHolder.getSheet(); return writeSheetHolder.getSheet();

3
update.md

@ -1,3 +1,6 @@
# 2.0.0-beta3
* 导出完成移除临时目录 [Issue #386](https://github.com/alibaba/easyexcel/issues/386)
# 2.0.0-beta2 # 2.0.0-beta2
* 加速gc回收 [Issue #511](https://github.com/alibaba/easyexcel/issues/511) * 加速gc回收 [Issue #511](https://github.com/alibaba/easyexcel/issues/511)
* 修改空字符串读取可能读取上个字段的数据的bug * 修改空字符串读取可能读取上个字段的数据的bug

Loading…
Cancel
Save