From 76282573d6900b3d17101be49998e7d3270d47e5 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 29 Aug 2019 19:46:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6=E6=B5=81?= =?UTF-8?q?=E9=9C=80=E8=A6=81gc=E6=89=8D=E8=83=BD=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20#511?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../excel/analysis/ExcelAnalyserImpl.java | 14 +++++ .../excel/analysis/v03/XlsSaxAnalyser.java | 1 + .../excel/analysis/v07/XlsxSaxAnalyser.java | 1 + .../metadata/holder/ReadWorkbookHolder.java | 26 +++++++++ .../easyexcel/test/temp/Lock2Test.java | 54 +++++++++++++++++++ .../easyexcel/test/temp/poi/Poi2Test.java | 50 +++++++++++++++++ update.md | 3 ++ 8 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi2Test.java diff --git a/pom.xml b/pom.xml index f85fd407..0e5b7909 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.0.0-beta1 + 2.0.0-beta2 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index dcf1d844..256fb517 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java @@ -131,6 +131,20 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { } catch (Throwable e) { throw new ExcelAnalysisException("Can not close IO", e); } + try { + if (readWorkbookHolder.getOpcPackage() != null) { + readWorkbookHolder.getOpcPackage().close(); + } + } catch (Throwable e) { + throw new ExcelAnalysisException("Can not close IO", e); + } + try { + if (readWorkbookHolder.getPoifsFileSystem() != null) { + readWorkbookHolder.getPoifsFileSystem().close(); + } + } catch (Throwable e) { + throw new ExcelAnalysisException("Can not close IO", e); + } } @Override diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java index 996e33ae..2b67b439 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java @@ -73,6 +73,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor { this.analysisContext = context; this.records = new TreeMap(); this.poifsFileSystem = poifsFileSystem; + analysisContext.readWorkbookHolder().setPoifsFileSystem(poifsFileSystem); } @Override diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java index 748306a7..f16f9267 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java @@ -59,6 +59,7 @@ public class XlsxSaxAnalyser implements ExcelExecutor { ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); OPCPackage pkg = readOpcPackage(readWorkbookHolder, decryptedStream); + readWorkbookHolder.setOpcPackage(pkg); PackagePart sharedStringsTablePackagePart = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType()).get(0); diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java index bfcc0506..9b77c8c2 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java @@ -6,6 +6,8 @@ import java.io.InputStream; import java.util.HashSet; import java.util.Set; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -90,6 +92,14 @@ public class ReadWorkbookHolder extends AbstractReadHolder { * Prevent repeating sheet */ private Set hasReadSheet; + /** + * Package + */ + private OPCPackage opcPackage; + /** + * File System + */ + private POIFSFileSystem poifsFileSystem; public ReadWorkbookHolder(ReadWorkbook readWorkbook) { super(readWorkbook, null, readWorkbook.getConvertAllFiled()); @@ -232,6 +242,22 @@ public class ReadWorkbookHolder extends AbstractReadHolder { this.defaultReturnMap = defaultReturnMap; } + public OPCPackage getOpcPackage() { + return opcPackage; + } + + public void setOpcPackage(OPCPackage opcPackage) { + this.opcPackage = opcPackage; + } + + public POIFSFileSystem getPoifsFileSystem() { + return poifsFileSystem; + } + + public void setPoifsFileSystem(POIFSFileSystem poifsFileSystem) { + this.poifsFileSystem = poifsFileSystem; + } + @Override public HolderEnum holderType() { return HolderEnum.WORKBOOK; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java new file mode 100644 index 00000000..dd891055 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java @@ -0,0 +1,54 @@ +package com.alibaba.easyexcel.test.temp; + +import java.io.File; +import java.io.FileInputStream; +import java.util.List; + +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.support.ExcelTypeEnum; +import com.alibaba.fastjson.JSON; + +/** + * 临时测试 + * + * @author Jiaju Zhuang + **/ +@Ignore +public class Lock2Test { + private static final Logger LOGGER = LoggerFactory.getLogger(Lock2Test.class); + + @Test + public void test() throws Exception { + File file = new File("D:\\test\\珠海2.xlsx"); + + List list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync(); + LOGGER.info("数据:{}", list.size()); + for (Object data : list) { + LOGGER.info("返回数据:{}", JSON.toJSONString(data)); + } + LOGGER.info("文件状态:{}", file.exists()); + file.delete(); + Thread.sleep(500 * 1000); + } + + @Test + public void test2() throws Exception { + File file = new File("D:\\test\\converter03.xls"); + + List list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync(); + LOGGER.info("数据:{}", list.size()); + for (Object data : list) { + LOGGER.info("返回数据:{}", JSON.toJSONString(data)); + } + LOGGER.info("文件状态:{}", file.exists()); + file.delete(); + Thread.sleep(500 * 1000); + } + + +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi2Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi2Test.java new file mode 100644 index 00000000..0de7d9e7 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi2Test.java @@ -0,0 +1,50 @@ +package com.alibaba.easyexcel.test.temp.poi; + +import java.io.File; +import java.io.IOException; + +import org.apache.poi.xssf.streaming.SXSSFRow; +import org.apache.poi.xssf.streaming.SXSSFSheet; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFRow; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.easyexcel.test.util.TestFileUtil; + +/** + * 测试poi + * + * @author Jiaju Zhuang + **/ +@Ignore +public class Poi2Test { + private static final Logger LOGGER = LoggerFactory.getLogger(Poi2Test.class); + + @Test + public void test() throws IOException { + String file = "D:\\test\\珠海.xlsx"; + SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file)); + SXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); + LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); + SXSSFRow row = xssfSheet.getRow(0); + LOGGER.info("第一行数据:{}", row); + } + + @Test + public void lastRowNumXSSF() throws IOException { + String file = "D:\\test\\珠海.xlsx"; + XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file); + LOGGER.info("一共:{}个sheet", xssfWorkbook.getNumberOfSheets()); + XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); + LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); + XSSFRow row = xssfSheet.getRow(0); + LOGGER.info("第一行数据:{}", row); + xssfSheet.createRow(20); + LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); + } +} diff --git a/update.md b/update.md index ec03f865..b1f71076 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 2.0.0-beta2 +* 加速gc回收 #511 + # 2.0.0-beta1 * 优化读写逻辑 * 优化读写对外接口