Browse Source

修改填充数据空数据的bug #1274

pull/1276/head v2.2.3
Jiaju Zhuang 5 years ago
parent
commit
fba1f72daa
  1. 2
      pom.xml
  2. 11
      src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java
  3. 16
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  4. 13
      src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
  5. 4
      update.md

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>2.2.2</version> <version>2.2.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>

11
src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java

@ -1,11 +0,0 @@
package com.alibaba.excel.converters;
/**
* When implementing <code>convertToExcelData</code> method, pay attention to the reference <code>value</code> may be
* null
*
* @author JiaJu Zhuang
**/
public interface NullableObjectConverter<T> extends Converter<T> {
}

16
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.context.WriteContext;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.NullableObjectConverter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellData; 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, protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
ExcelContentProperty excelContentProperty, Head head, Integer relativeRowIndex) { ExcelContentProperty excelContentProperty, Head head, Integer relativeRowIndex) {
boolean needTrim = if (value == null) {
value != null && (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()); return new CellData(CellDataTypeEnum.EMPTY);
if (needTrim) { }
value = ((String) value).trim(); if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) {
value = ((String)value).trim();
} }
CellData cellData = convert(currentWriteHolder, clazz, cell, value, excelContentProperty); CellData cellData = convert(currentWriteHolder, clazz, cell, value, excelContentProperty);
if (cellData.getFormula() != null && cellData.getFormula()) { 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, protected CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
ExcelContentProperty excelContentProperty) { ExcelContentProperty excelContentProperty) {
if (value == null) {
return new CellData(CellDataTypeEnum.EMPTY);
}
// This means that the user has defined the data. // This means that the user has defined the data.
if (value instanceof CellData) { if (value instanceof CellData) {
CellData cellDataValue = (CellData)value; CellData cellDataValue = (CellData)value;
@ -107,9 +110,6 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
new CellData(CellDataTypeEnum.EMPTY), excelContentProperty, new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
"Can not find 'Converter' support class " + clazz.getSimpleName() + "."); "Can not find 'Converter' support class " + clazz.getSimpleName() + ".");
} }
if (value == null && !(converter instanceof NullableObjectConverter)) {
return new CellData(CellDataTypeEnum.EMPTY);
}
CellData cellData; CellData cellData;
try { try {
cellData = cellData =

13
src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java

@ -147,11 +147,11 @@ public class FillDataTest {
excelWriter.finish(); excelWriter.finish();
List<Object> list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync(); List<Object> list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync();
Map<String, String> map0 = (Map<String, String>)list.get(0); Map<String, String> map0 = (Map<String, String>) list.get(0);
Assert.assertEquals("张三", map0.get(21)); Assert.assertEquals("张三", map0.get(21));
Map<String, String> map27 = (Map<String, String>)list.get(27); Map<String, String> map27 = (Map<String, String>) list.get(27);
Assert.assertEquals("张三", map27.get(0)); Assert.assertEquals("张三", map27.get(0));
Map<String, String> map29 = (Map<String, String>)list.get(29); Map<String, String> map29 = (Map<String, String>) list.get(29);
Assert.assertEquals("张三", map29.get(3)); Assert.assertEquals("张三", map29.get(3));
} }
@ -168,7 +168,7 @@ public class FillDataTest {
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync(); List<Object> list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync();
Assert.assertEquals(list.size(), 5L); Assert.assertEquals(list.size(), 5L);
Map<String, String> map0 = (Map<String, String>)list.get(0); Map<String, String> map0 = (Map<String, String>) list.get(0);
Assert.assertEquals("张三", map0.get(2)); Assert.assertEquals("张三", map0.get(2));
} }
@ -185,7 +185,7 @@ public class FillDataTest {
excelWriter.finish(); excelWriter.finish();
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync(); List<Object> list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync();
Assert.assertEquals(list.size(), 21L); Assert.assertEquals(list.size(), 21L);
Map<String, String> map19 = (Map<String, String>)list.get(19); Map<String, String> map19 = (Map<String, String>) list.get(19);
Assert.assertEquals("张三", map19.get(0)); Assert.assertEquals("张三", map19.get(0));
} }
@ -203,6 +203,9 @@ public class FillDataTest {
list.add(fillData); list.add(fillData);
fillData.setName("张三"); fillData.setName("张三");
fillData.setNumber(5.2); fillData.setNumber(5.2);
if (i == 5) {
fillData.setName(null);
}
} }
return list; return list;
} }

4
update.md

@ -1,3 +1,7 @@
# 2.2.3
* 修改填充数据空数据的bug [Issue #1274](https://github.com/alibaba/easyexcel/issues/1274)
* 回退自定义转换器入参为空
# 2.2.2 # 2.2.2
* 修改`sheet`事件未调用的bug * 修改`sheet`事件未调用的bug

Loading…
Cancel
Save