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