diff --git a/pom.xml b/pom.xml index bb5182d..783f120 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 43b1cb2..0000000 --- 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 12250f1..4eb115c 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 5c40030..3df12b1 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/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java new file mode 100644 index 0000000..c762db7 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java @@ -0,0 +1,280 @@ +package com.alibaba.easyexcel.test.core.handler; + +import java.util.List; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.junit.Assert; + +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.handler.RowWriteHandler; +import com.alibaba.excel.write.handler.SheetWriteHandler; +import com.alibaba.excel.write.handler.WorkbookWriteHandler; +import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; +import com.alibaba.excel.write.metadata.holder.WriteTableHolder; +import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; + +/** + * + * + * @author JiaJu Zhuang + **/ +public class WriteHandler implements WorkbookWriteHandler, SheetWriteHandler, RowWriteHandler, CellWriteHandler { + + private long beforeCellCreate = 0L; + private long afterCellCreate = 0L; + private long afterCellDataConverted = 0L; + private long afterCellDispose = 0L; + private long beforeRowCreate = 0L; + private long afterRowCreate = 0L; + private long afterRowDispose = 0L; + private long beforeSheetCreate = 0L; + private long afterSheetCreate = 0L; + private long beforeWorkbookCreate = 0L; + private long afterWorkbookCreate = 0L; + private long afterWorkbookDispose = 0L; + + + @Override + public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, + Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + beforeCellCreate++; + } + + } + + @Override + public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, + Head head, Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterCellCreate++; + } + } + + @Override + public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, + CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(1L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(1, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(1L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterCellDataConverted++; + } + + @Override + public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, + List cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(1L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterCellDispose++; + } + } + + @Override + public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer rowIndex, + Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(0L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + beforeRowCreate++; + } + + } + + @Override + public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, + Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterRowCreate++; + } + } + + @Override + public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, + Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(1L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(1L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterRowDispose++; + } + } + + @Override + public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(0L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(0L, beforeSheetCreate); + Assert.assertEquals(0L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + beforeSheetCreate++; + } + + @Override + public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(0L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(0L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterSheetCreate++; + } + + @Override + public void beforeWorkbookCreate() { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(0L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(0L, beforeSheetCreate); + Assert.assertEquals(0L, afterSheetCreate); + Assert.assertEquals(0L, beforeWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + beforeWorkbookCreate++; + } + + @Override + public void afterWorkbookCreate(WriteWorkbookHolder writeWorkbookHolder) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(0L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(0L, beforeSheetCreate); + Assert.assertEquals(0L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterWorkbookCreate++; + } + + @Override + public void afterWorkbookDispose(WriteWorkbookHolder writeWorkbookHolder) { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(1L, afterCellCreate); + Assert.assertEquals(1L, afterCellDataConverted); + Assert.assertEquals(1L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(1L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterWorkbookDispose++; + + } + + public void afterAll() { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(1L, afterCellCreate); + Assert.assertEquals(1L, afterCellDataConverted); + Assert.assertEquals(1L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(1L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookDispose); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java new file mode 100644 index 0000000..ad2188a --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java @@ -0,0 +1,14 @@ +package com.alibaba.easyexcel.test.core.handler; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.Data; + +/** + * @author Jiaju Zhuang + */ +@Data +public class WriteHandlerData { + @ExcelProperty("姓名") + private String name; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java new file mode 100644 index 0000000..a96d133 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java @@ -0,0 +1,96 @@ +package com.alibaba.easyexcel.test.core.handler; + +import java.io.File; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import com.alibaba.easyexcel.test.core.head.ListHeadDataListener; +import com.alibaba.easyexcel.test.core.simple.SimpleData; +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.util.DateUtils; +import com.alibaba.excel.write.handler.WorkbookWriteHandler; + +/** + * + * @author Jiaju Zhuang + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class WriteHandlerTest { + + private static File file07; + private static File file03; + + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("writeHandler07.xlsx"); + file03 = TestFileUtil.createNewFile("writeHandler03.xls"); + } + + @Test + public void t01WorkbookWrite07() throws Exception { + workbookWrite(file07); + } + + @Test + public void t02WorkbookWrite03() throws Exception { + workbookWrite(file03); + } + + @Test + public void t03SheetWrite07() throws Exception { + sheetWrite(file07); + } + + @Test + public void t04SheetWrite03() throws Exception { + sheetWrite(file03); + } + + @Test + public void t05TableWrite07() throws Exception { + workbookWrite(file07); + tableWrite(file07); + } + + @Test + public void t06TableWrite03() throws Exception { + tableWrite(file03); + } + + + private void workbookWrite(File file) { + WriteHandler writeHandler = new WriteHandler(); + EasyExcel.write(file).head(WriteHandlerData.class).registerWriteHandler(writeHandler).sheet().doWrite(data()); + writeHandler.afterAll(); + } + + private void sheetWrite(File file) { + WriteHandler writeHandler = new WriteHandler(); + EasyExcel.write(file).head(WriteHandlerData.class).sheet().registerWriteHandler(writeHandler).doWrite(data()); + writeHandler.afterAll(); + } + + private void tableWrite(File file) { + WriteHandler writeHandler = new WriteHandler(); + EasyExcel.write(file).head(WriteHandlerData.class).sheet().table(0).registerWriteHandler(writeHandler) + .doWrite(data()); + writeHandler.afterAll(); + } + + private List data() { + List list = new ArrayList(); + for (int i = 0; i < 1; i++) { + WriteHandlerData data = new WriteHandlerData(); + data.setName("姓名" + i); + list.add(data); + } + return list; + } +} diff --git a/update.md b/update.md index dd54a2a..fdb7b14 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