Browse Source

修复文件流需要gc才能关闭的问题 #511

pull/523/head
Jiaju Zhuang 5 years ago
parent
commit
76282573d6
  1. 2
      pom.xml
  2. 14
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  3. 1
      src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  4. 1
      src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  5. 26
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java
  6. 54
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  7. 50
      src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi2Test.java
  8. 3
      update.md

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>2.0.0-beta1</version> <version>2.0.0-beta2</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>

14
src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java

@ -131,6 +131,20 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
} catch (Throwable e) { } catch (Throwable e) {
throw new ExcelAnalysisException("Can not close IO", 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 @Override

1
src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java

@ -73,6 +73,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor {
this.analysisContext = context; this.analysisContext = context;
this.records = new TreeMap<Integer, CellData>(); this.records = new TreeMap<Integer, CellData>();
this.poifsFileSystem = poifsFileSystem; this.poifsFileSystem = poifsFileSystem;
analysisContext.readWorkbookHolder().setPoifsFileSystem(poifsFileSystem);
} }
@Override @Override

1
src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java

@ -59,6 +59,7 @@ public class XlsxSaxAnalyser implements ExcelExecutor {
ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder();
OPCPackage pkg = readOpcPackage(readWorkbookHolder, decryptedStream); OPCPackage pkg = readOpcPackage(readWorkbookHolder, decryptedStream);
readWorkbookHolder.setOpcPackage(pkg);
PackagePart sharedStringsTablePackagePart = PackagePart sharedStringsTablePackagePart =
pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType()).get(0); pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType()).get(0);

26
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.HashSet;
import java.util.Set; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -90,6 +92,14 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
* Prevent repeating sheet * Prevent repeating sheet
*/ */
private Set<Integer> hasReadSheet; private Set<Integer> hasReadSheet;
/**
* Package
*/
private OPCPackage opcPackage;
/**
* File System
*/
private POIFSFileSystem poifsFileSystem;
public ReadWorkbookHolder(ReadWorkbook readWorkbook) { public ReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook, null, readWorkbook.getConvertAllFiled()); super(readWorkbook, null, readWorkbook.getConvertAllFiled());
@ -232,6 +242,22 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
this.defaultReturnMap = defaultReturnMap; 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 @Override
public HolderEnum holderType() { public HolderEnum holderType() {
return HolderEnum.WORKBOOK; return HolderEnum.WORKBOOK;

54
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<Object> 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<Object> 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);
}
}

50
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());
}
}

3
update.md

@ -1,3 +1,6 @@
# 2.0.0-beta2
* 加速gc回收 #511
# 2.0.0-beta1 # 2.0.0-beta1
* 优化读写逻辑 * 优化读写逻辑
* 优化读写对外接口 * 优化读写对外接口

Loading…
Cancel
Save