Browse Source

Merge remote-tracking branch 'remotes/origin/master' into 2.1.x

# Conflicts:
#	src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java
#	update.md
2.1.x
Jiaju Zhuang 5 years ago
parent
commit
0233e34e25
  1. 2
      pom.xml
  2. 4
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  3. 15
      src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java
  4. 15
      src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java
  5. 6
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java
  6. 9
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  7. 8
      src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java
  8. 3
      update.md

2
pom.xml

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

4
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -451,7 +451,9 @@ public class WriteContextImpl implements WriteContext {
OutputStream outputStream = encryptor.getDataStream(fileSystem);
opcPackage.save(outputStream);
} finally {
opcPackage.close();
if (opcPackage != null) {
opcPackage.close();
}
}
return fileSystem;
}

15
src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java

@ -10,6 +10,8 @@ import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.cache.selector.ReadCacheSelector;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ModelBuildEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.support.ExcelTypeEnum;
@ -156,6 +158,19 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce
return this;
}
/**
* Whether to use the default listener, which is used by default.
* <p>
* The {@link ModelBuildEventListener} is loaded by default to convert the object.
*
* @param useDefaultListener
* @return
*/
public ExcelReaderBuilder useDefaultListener(Boolean useDefaultListener) {
readWorkbook.setUseDefaultListener(useDefaultListener);
return this;
}
public ExcelReader build() {
return new ExcelReader(readWorkbook);
}

15
src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java

@ -9,6 +9,7 @@ import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.cache.selector.ReadCacheSelector;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ModelBuildEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
/**
@ -76,6 +77,12 @@ public class ReadWorkbook extends ReadBasicParameter {
* @see SAXParserFactory#newInstance(String, ClassLoader)
*/
private String xlsxSAXParserFactoryName;
/**
* Whether to use the default listener, which is used by default.
* <p>
* The {@link ModelBuildEventListener} is loaded by default to convert the object.
*/
private Boolean useDefaultListener;
/**
* The default is all excel objects.Default is true.
* <p>
@ -197,4 +204,12 @@ public class ReadWorkbook extends ReadBasicParameter {
public void setXlsxSAXParserFactoryName(String xlsxSAXParserFactoryName) {
this.xlsxSAXParserFactoryName = xlsxSAXParserFactoryName;
}
public Boolean getUseDefaultListener() {
return useDefaultListener;
}
public void setUseDefaultListener(Boolean useDefaultListener) {
this.useDefaultListener = useDefaultListener;
}
}

6
src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java

@ -26,6 +26,7 @@ import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.listener.ReadListenerRegistryCenter;
import com.alibaba.excel.read.listener.event.AnalysisFinishEvent;
import com.alibaba.excel.read.metadata.ReadBasicParameter;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.ConverterUtils;
@ -91,7 +92,10 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
this.readListenerList = new ArrayList<ReadListener>(parentAbstractReadHolder.getReadListenerList());
}
if (HolderEnum.WORKBOOK.equals(holderType())) {
readListenerList.add(new ModelBuildEventListener());
Boolean useDefaultListener = ((ReadWorkbook)readBasicParameter).getUseDefaultListener();
if (useDefaultListener == null || useDefaultListener) {
readListenerList.add(new ModelBuildEventListener());
}
}
if (readBasicParameter.getCustomReadListenerList() != null
&& !readBasicParameter.getCustomReadListenerList().isEmpty()) {

9
src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java

@ -1,7 +1,6 @@
package com.alibaba.easyexcel.test.temp;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.junit.Ignore;
@ -9,13 +8,7 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.demo.read.DemoData;
import com.alibaba.easyexcel.test.demo.read.DemoDataListener;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
/**
@ -43,7 +36,7 @@ public class Lock2Test {
// 写法1:
String fileName = "D:\\test\\珠海 (1).xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
EasyExcel.read(fileName, LockData.class, new LockDataListener()).sheet().doRead();
EasyExcel.read(fileName, LockData.class, new LockDataListener()).useDefaultListener(false).sheet().doRead();
}
@Test

8
src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java

@ -2,6 +2,7 @@ package com.alibaba.easyexcel.test.temp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -9,6 +10,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.demo.read.DemoDataListener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.fastjson.JSON;
/**
@ -16,16 +18,16 @@ import com.alibaba.fastjson.JSON;
*
* @author Jiaju Zhuang
*/
public class LockDataListener extends AnalysisEventListener<LockData> {
public class LockDataListener extends AnalysisEventListener<Map<Integer, CellData>> {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
/**
* 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收
*/
private static final int BATCH_COUNT = 5;
List<LockData> list = new ArrayList<LockData>();
List<Map<Integer, CellData>> list = new ArrayList<Map<Integer, CellData>>();
@Override
public void invoke(LockData data, AnalysisContext context) {
public void invoke(Map<Integer, CellData> data, AnalysisContext context) {
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
list.add(data);
if (list.size() >= BATCH_COUNT) {

3
update.md

@ -4,6 +4,9 @@
* 修复xls 用Map接收时多次接收会是同一个对象的bug
* 修复浮点型数据导入到excel 会丢失精度的bug
# 2.1.4
* 新增参数`useDefaultListener` 可以排除默认对象转换
# 2.1.3
* 每个java进程单独创建一个缓存目录 [Issue #813](https://github.com/alibaba/easyexcel/issues/813)
* 统一修改合并为unsafe,提高大量数据导出的合并的效率

Loading…
Cancel
Save