From ae63dd2ae06e478e9ec19f36401ff394aef02f51 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 22 Apr 2020 21:13:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=BD=AC=E6=8D=A2=E5=99=A8=20=E5=85=A5?= =?UTF-8?q?=E5=8F=82=E5=8F=AF=E4=BB=A5=E4=B8=BA=E7=A9=BA=20#1084?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converters/NullableObjectConverter.java | 11 +++++++++++ .../executor/AbstractExcelWriteExecutor.java | 16 ++++++++-------- .../easyexcel/test/temp/read/HeadListener.java | 4 ++-- .../easyexcel/test/temp/read/HeadReadData.java | 5 ++--- .../easyexcel/test/temp/read/HeadReadTest.java | 4 ++-- update.md | 1 + 6 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java diff --git a/src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java b/src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java new file mode 100644 index 00000000..43b1cb2c --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java @@ -0,0 +1,11 @@ +package com.alibaba.excel.converters; + +/** + * When implementing convertToExcelData method, pay attention to the reference value may be + * null + * + * @author JiaJu Zhuang + **/ +public interface NullableObjectConverter extends Converter { + +} diff --git a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java index 4eb115c5..12250f19 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -10,6 +10,7 @@ import org.apache.poi.ss.usermodel.Sheet; import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.ConverterKeyBuild; +import com.alibaba.excel.converters.NullableObjectConverter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.metadata.CellData; @@ -32,11 +33,10 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value, ExcelContentProperty excelContentProperty, Head head, Integer relativeRowIndex) { - if (value == null) { - return new CellData(CellDataTypeEnum.EMPTY); - } - if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) { - value = ((String)value).trim(); + boolean needTrim = + value != null && (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()); + if (needTrim) { + value = ((String) value).trim(); } CellData cellData = convert(currentWriteHolder, clazz, cell, value, excelContentProperty); if (cellData.getFormula() != null && cellData.getFormula()) { @@ -70,9 +70,6 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { protected CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value, ExcelContentProperty excelContentProperty) { - if (value == null) { - return new CellData(CellDataTypeEnum.EMPTY); - } // This means that the user has defined the data. if (value instanceof CellData) { CellData cellDataValue = (CellData)value; @@ -110,6 +107,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { new CellData(CellDataTypeEnum.EMPTY), excelContentProperty, "Can not find 'Converter' support class " + clazz.getSimpleName() + "."); } + if (value == null && !(converter instanceof NullableObjectConverter)) { + return new CellData(CellDataTypeEnum.EMPTY); + } CellData cellData; try { cellData = diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java index 01fbb466..31875fe2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java @@ -14,7 +14,7 @@ import com.alibaba.fastjson.JSON; * * @author Jiaju Zhuang */ -public class HeadListener extends AnalysisEventListener> { +public class HeadListener extends AnalysisEventListener { private static final Logger LOGGER = LoggerFactory.getLogger(HeadListener.class); /** * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 @@ -29,7 +29,7 @@ public class HeadListener extends AnalysisEventListener> { } @Override - public void invoke(Map data, AnalysisContext context) { + public void invoke(HeadReadData data, AnalysisContext context) { LOGGER.info("index:{}", context.readRowHolder().getRowIndex()); LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); } diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java index 538e2663..62d5e317 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java @@ -11,10 +11,9 @@ import lombok.experimental.Accessors; * @author Jiaju Zhuang **/ @Data -@Accessors(chain = true) public class HeadReadData { - @ExcelProperty("头1") + @ExcelProperty({"主标题","数据1"}) private String h1; - @ExcelProperty({"头", "头2"}) + @ExcelProperty({"主标题", "数据2"}) private String h2; } diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java index 98c411b6..40aa4b8f 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java @@ -32,8 +32,8 @@ public class HeadReadTest { @Test public void test() throws Exception { - File file = TestFileUtil.readUserHomeFile("test/t1.xlsx"); - EasyExcel.read(file, null, new HeadListener()).ignoreEmptyRow(false).sheet(0).doRead(); + File file = TestFileUtil.readUserHomeFile("test/t2.xlsx"); + EasyExcel.read(file, HeadReadData.class, new HeadListener()).ignoreEmptyRow(false).sheet(0).doRead(); } diff --git a/update.md b/update.md index a4b9a75a..b4a53e7f 100644 --- a/update.md +++ b/update.md @@ -2,6 +2,7 @@ * 发布正式版 * 修复第一行为空不会调用`invokeHeadMap`的bug [Issue #993](https://github.com/alibaba/easyexcel/issues/993) * 当类的属性没有按照ExcelProperty的属性index顺序排序的时候,写数据出现错乱 [Issue #1046](https://github.com/alibaba/easyexcel/issues/1046) +* 新增支持自定义转换器 入参可以为空 实现`NullableObjectConverter` 即可 [Issue #1084](https://github.com/alibaba/easyexcel/issues/1084) # 2.2.0-beta2 * 修复最长匹配策略不同表格会有影响的bug [Issue #1010](https://github.com/alibaba/easyexcel/issues/1010)