From fba1f72daafdc2ce348af9d9828b919451436db9 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Mon, 27 Apr 2020 19:08:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A1=AB=E5=85=85=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=A9=BA=E6=95=B0=E6=8D=AE=E7=9A=84bug=20#1274?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../converters/NullableObjectConverter.java | 11 ----------- .../executor/AbstractExcelWriteExecutor.java | 16 ++++++++-------- .../easyexcel/test/core/fill/FillDataTest.java | 13 ++++++++----- update.md | 4 ++++ 5 files changed, 21 insertions(+), 25 deletions(-) delete mode 100644 src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java diff --git a/pom.xml b/pom.xml index bb5182d8..783f1207 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.2.2 + 2.2.3 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java b/src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java deleted file mode 100644 index 43b1cb2c..00000000 --- a/src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java +++ /dev/null @@ -1,11 +0,0 @@ -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 12250f19..4eb115c5 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -10,7 +10,6 @@ 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; @@ -33,10 +32,11 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value, ExcelContentProperty excelContentProperty, Head head, Integer relativeRowIndex) { - boolean needTrim = - value != null && (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()); - if (needTrim) { - value = ((String) value).trim(); + if (value == null) { + return new CellData(CellDataTypeEnum.EMPTY); + } + if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) { + value = ((String)value).trim(); } CellData cellData = convert(currentWriteHolder, clazz, cell, value, excelContentProperty); if (cellData.getFormula() != null && cellData.getFormula()) { @@ -70,6 +70,9 @@ 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; @@ -107,9 +110,6 @@ 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/core/fill/FillDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java index 5c400305..3df12b1c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java @@ -147,11 +147,11 @@ public class FillDataTest { excelWriter.finish(); List list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync(); - Map map0 = (Map)list.get(0); + Map map0 = (Map) list.get(0); Assert.assertEquals("张三", map0.get(21)); - Map map27 = (Map)list.get(27); + Map map27 = (Map) list.get(27); Assert.assertEquals("张三", map27.get(0)); - Map map29 = (Map)list.get(29); + Map map29 = (Map) list.get(29); Assert.assertEquals("张三", map29.get(3)); } @@ -168,7 +168,7 @@ public class FillDataTest { List list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync(); Assert.assertEquals(list.size(), 5L); - Map map0 = (Map)list.get(0); + Map map0 = (Map) list.get(0); Assert.assertEquals("张三", map0.get(2)); } @@ -185,7 +185,7 @@ public class FillDataTest { excelWriter.finish(); List list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync(); Assert.assertEquals(list.size(), 21L); - Map map19 = (Map)list.get(19); + Map map19 = (Map) list.get(19); Assert.assertEquals("张三", map19.get(0)); } @@ -203,6 +203,9 @@ public class FillDataTest { list.add(fillData); fillData.setName("张三"); fillData.setNumber(5.2); + if (i == 5) { + fillData.setName(null); + } } return list; } diff --git a/update.md b/update.md index dd54a2af..fdb7b140 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,7 @@ +# 2.2.3 +* 修改填充数据空数据的bug [Issue #1274](https://github.com/alibaba/easyexcel/issues/1274) +* 回退自定义转换器入参为空 + # 2.2.2 * 修改`sheet`事件未调用的bug