Skip to content

Commit

Permalink
新增支持自定义转换器 入参可以为空 #1084
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Apr 22, 2020
1 parent a7db3cf commit ae63dd2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand All @@ -32,11 +33,10 @@ public AbstractExcelWriteExecutor(WriteContext writeContext) {

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()) {
Expand Down Expand Up @@ -70,9 +70,6 @@ protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz,

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;
Expand Down Expand Up @@ -110,6 +107,9 @@ private CellData doConvert(WriteHolder currentWriteHolder, Class clazz, Cell cel
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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @author Jiaju Zhuang
*/
public class HeadListener extends AnalysisEventListener<Map<Integer,String>> {
public class HeadListener extends AnalysisEventListener<HeadReadData> {
private static final Logger LOGGER = LoggerFactory.getLogger(HeadListener.class);
/**
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
Expand All @@ -29,7 +29,7 @@ public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context)
}

@Override
public void invoke(Map<Integer,String> data, AnalysisContext context) {
public void invoke(HeadReadData data, AnalysisContext context) {
LOGGER.info("index:{}", context.readRowHolder().getRowIndex());
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
* @author Jiaju Zhuang
**/
@Data
@Accessors(chain = true)
public class HeadReadData {
@ExcelProperty("头1")
@ExcelProperty({"主标题","数据1"})
private String h1;
@ExcelProperty({"", "头2"})
@ExcelProperty({"主标题", "数据2"})
private String h2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public void testread() throws Exception {

@Test
public void test() throws Exception {
File file = TestFileUtil.readUserHomeFile("test/t1.xlsx");
EasyExcel.read(file, null, new HeadListener()).ignoreEmptyRow(false).sheet(0).doRead();
File file = TestFileUtil.readUserHomeFile("test/t2.xlsx");
EasyExcel.read(file, HeadReadData.class, new HeadListener()).ignoreEmptyRow(false).sheet(0).doRead();

}

Expand Down
1 change: 1 addition & 0 deletions update.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* 发布正式版
* 修复第一行为空不会调用`invokeHeadMap`的bug [Issue #993](https://github.com/alibaba/easyexcel/issues/993)
* 当类的属性没有按照ExcelProperty的属性index顺序排序的时候,写数据出现错乱 [Issue #1046](https://github.com/alibaba/easyexcel/issues/1046)
* 新增支持自定义转换器 入参可以为空 实现`NullableObjectConverter` 即可 [Issue #1084](https://github.com/alibaba/easyexcel/issues/1084)

# 2.2.0-beta2
* 修复最长匹配策略不同表格会有影响的bug [Issue #1010](https://github.com/alibaba/easyexcel/issues/1010)
Expand Down

0 comments on commit ae63dd2

Please sign in to comment.