Browse Source

Merge pull request #1276 from alibaba/developing

Developing
developing
Jiaju Zhuang 5 years ago committed by GitHub
parent
commit
3c373b3c1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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. 280
      src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java
  6. 14
      src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java
  7. 96
      src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java
  8. 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;
} }

280
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<CellData> 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);
}
}

14
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;
}

96
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<WriteHandlerData> data() {
List<WriteHandlerData> list = new ArrayList<WriteHandlerData>();
for (int i = 0; i < 1; i++) {
WriteHandlerData data = new WriteHandlerData();
data.setName("姓名" + i);
list.add(data);
}
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