From 269240d8e91c7e66d5c322f379bb372fc039eaa8 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 13 Apr 2021 15:45:39 +0800 Subject: [PATCH] =?UTF-8?q?`Converter`=E6=94=AF=E6=8C=81null=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=20[Issue=20#1776]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/converters/NullableObjectConverter.java | 10 ++++++++++ .../write/executor/AbstractExcelWriteExecutor.java | 14 +++++++------- update.md | 2 ++ 3 files changed, 19 insertions(+), 7 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..fe24f63d --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java @@ -0,0 +1,10 @@ +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 b20305f6..b45082c0 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -3,6 +3,7 @@ package com.alibaba.excel.write.executor; 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 +34,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, CellDataTypeEnum targetType, 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()) { + boolean needTrim = value != null && (value instanceof String && currentWriteHolder.globalConfiguration() + .getAutoTrim()); + if (needTrim) { value = ((String)value).trim(); } CellData cellData = convert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); @@ -75,9 +75,6 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { protected CellData convert(WriteHolder currentWriteHolder, Class clazz, CellDataTypeEnum targetType, 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; @@ -115,6 +112,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 = converter.convertToExcelData(value, excelContentProperty, currentWriteHolder); diff --git a/update.md b/update.md index 09ca62ac..db5c7cb7 100644 --- a/update.md +++ b/update.md @@ -40,6 +40,8 @@ * 修复填充的时候,最后一行中间有空行会创建失败的bug * 修复`includeColumnIndexes`不包含第列 会无法导出数据的bug [Issue #1346](https://github.com/alibaba/easyexcel/issues/1346) * 修复`@NumberFormat`注解转换double时可能会丢失精度 [Issue #1306](https://github.com/alibaba/easyexcel/issues/1306) +* `Converter`支持null转换 [Issue #1776](https://github.com/alibaba/easyexcel/issues/1776) + # 2.2.3 * 修改填充数据空数据的bug [Issue #1274](https://github.com/alibaba/easyexcel/issues/1274)