From 569f42e0da55c99b3e4c0079cac46df0a748dc02 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 4 Dec 2019 19:12:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E`useDefaultListener`=20?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=BF=BD=E7=95=A5=E9=BB=98=E8=AE=A4=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/read/builder/ExcelReaderBuilder.java | 14 ++++++++++++++ .../alibaba/excel/read/metadata/ReadWorkbook.java | 15 +++++++++++++++ .../read/metadata/holder/AbstractReadHolder.java | 6 +++++- .../alibaba/easyexcel/test/temp/Lock2Test.java | 9 +-------- .../easyexcel/test/temp/LockDataListener.java | 8 +++++--- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java index 340548b..a458acf 100644 --- a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java +++ b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java @@ -11,6 +11,7 @@ import com.alibaba.excel.cache.selector.ReadCacheSelector; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; 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; @@ -233,6 +234,19 @@ public class ExcelReaderBuilder { return this; } + /** + * Whether to use the default listener, which is used by default. + *

+ * 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); } diff --git a/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java b/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java index 3a873da..6392ca0 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java +++ b/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java @@ -7,6 +7,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; /** @@ -63,6 +64,12 @@ public class ReadWorkbook extends ReadBasicParameter { * Whether the encryption */ private String password; + /** + * Whether to use the default listener, which is used by default. + *

+ * The {@link ModelBuildEventListener} is loaded by default to convert the object. + */ + private Boolean useDefaultListener; /** * The default is all excel objects.Default is true. *

@@ -176,4 +183,12 @@ public class ReadWorkbook extends ReadBasicParameter { public void setPassword(String password) { this.password = password; } + + public Boolean getUseDefaultListener() { + return useDefaultListener; + } + + public void setUseDefaultListener(Boolean useDefaultListener) { + this.useDefaultListener = useDefaultListener; + } } diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java index 02b2ef5..7133f99 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java +++ b/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(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()) { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java index 052b60b..f72e94d 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java +++ b/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 diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java b/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java index bda277c..d589111 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java +++ b/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 { +public class LockDataListener extends AnalysisEventListener> { private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class); /** * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 5; - List list = new ArrayList(); + List> list = new ArrayList>(); @Override - public void invoke(LockData data, AnalysisContext context) { + public void invoke(Map data, AnalysisContext context) { LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); list.add(data); if (list.size() >= BATCH_COUNT) { From 46266abccb54666af614a6f69ffd3511eaaf3e8b Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 4 Dec 2019 19:19:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E`useDefaultListener`=20?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=BF=BD=E7=95=A5=E9=BB=98=E8=AE=A4=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- update.md | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e85d044..415b45a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.1.3 + 2.1.4 jar easyexcel diff --git a/update.md b/update.md index 757de15..830a7d1 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 2.1.4 +* 新增参数`useDefaultListener` 可以排除默认对象转换 + # 2.1.3 * 每个java进程单独创建一个缓存目录 [Issue #813](https://github.com/alibaba/easyexcel/issues/813) * 统一修改合并为unsafe,提高大量数据导出的合并的效率