alibaba EasyExcel 属性注解
常用注解详解:
@NumberFormat还不清楚,待补充
注释:
1. 是否使用1904年日期系统
Excel 支持两个日期系统:1900年日期系统(推荐)和 1904年日期系统。每个日期系统使用日期作为计算的所有其他工作簿中的唯一开始日期。所有版本的 Excel for Windows 都计算基于 1900年日期系统中的日期。Excel 2008 for Mac 和早期 Excel for Mac 版本计算基于 1904年日期系统的日期。Excel 2016 for Mac 和 Excel for Mac 2011 使用 1900年日期系统,保证日期与 Excel for Windows 的兼容性
2. 自定义格式转换
Excel转换类
@Data
public class ConverterData {
/**
* converter属性定义自己的字符串转换器
*/
@ExcelProperty(converter = CustomStringConverter.class)
private String string;
/**
* 这里用string 去接日期才能格式化
*/
@DateTimeFormat("yyyy年MM月dd日 HH时mm分ss秒")
private String date;
/**
* 我想接收百分比的数字
*/
@NumberFormat("#.##%")
private String doubleData;
}
自定义的转换器
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
public class CustomStringConverter implements Converter<String> {
/**
* java中的数据格式
*/
@Override
public Class supportJavaTypeKey() {
return String.class;
}
/**
* Excel中的数据格式
*/
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
/**
* 这里读的时候会调用
*/
@Override
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return "自定义的字符:" + cellData.getStringValue();
}
/**
* 这里是写的时候会调用
*/
@Override
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return new CellData("自定义的字符:" + value);
}
}
3. 复杂头写入(合并单元格)
/**
* 主标题 将整合为一个单元格效果如下:
* —————————————————————————
* | 主标题 |
* —————————————————————————
* |字符串标题|日期标题|数字标题|
* —————————————————————————
*/
@ExcelProperty({
"主标题", "字符串标题"})
private String string;
@ExcelProperty({
"主标题", "日期标题"})
private Date date;
@ExcelProperty({
"主标题", "数字标题"})
private Double doubleData;
