easyexcel空值null值转换
下⾯我们实现对null字段的拦截处理
升级EasyExcel版本, 3.0.5可以不⽤导⼊poi-ooxml、和poi两个包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
转换器IntegerNullableConverter.java
package com.verter;
import l.converters.NullableObjectConverter;
import l.enums.CellDataTypeEnum;
import l.metadata.GlobalConfiguration;
import l.metadata.data.ReadCellData;
import l.metadata.data.WriteCellData;
import l.metadata.property.ExcelContentProperty;
import java.math.BigDecimal;
import java.util.Objects;
/**
* null转换
*/
public class IntegerNullableConverter implements NullableObjectConverter<Integer>{
@Override
public Class<Integer>supportJavaTypeKey(){
return Integer.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey(){
return CellDataTypeEnum.NUMBER;
}
@Override
public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)throws E xception {
NumberValue().intValue();
}
@Override
public WriteCellData<?>convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration)thro ws Exception {
if(Objects.isNull(integer)){
return new WriteCellData<>("-");
bigdecimal转换为integer}else{
return new WriteCellData<>(BigDecimal.valueOf(integer));
}
}
}
使⽤注解绑定转换器
@ExcelProperty(value ="测试null值转换", converter = IntegerNullableConverter.class)
// 单元格右对齐(默认int类型右对齐,字符串左对齐)
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.RIGHT)
private Integer testNull;
⽣成表格
/**
* 测试null值转换
*/
@Test
public void exportNullColumn(){
Consumer<ExcelWriter> consumer = writer ->{
List<Student> students =generateStudent(2);
<(0).setTestNull(100);
writer.write(students, EasyExcel.writerSheet("学⽣信息")
.registerWriteHandler(new FreezeNameHandler())// 冻结姓名列
.head(Student.class)
.build());
};
export("D:/报表.xlsx", consumer);
}
效果

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。