alibaba EasyExcel 属性注解

常用注解详解:
注解名称作用范围属性默认值描述
@ColumnWidth类或属性value(int)-1设置表格的列宽
@ContentRowHeightvalue(int)-1设置表格的高度(不含表头)
@HeadRowHeightvalue(int)-1设置表格表头的高度
@HeadFontStyle类或属性fontName(String)宋体设置表格表头的字体
fontHeightInPoints(short)14设置表格表头字体的大小
@ExcelIgnore属性转化表格时忽略该字段
@ExcelIgnoreUnannotated转化表格时忽略所有未注释的字段
@ExcelProperty属性value(String[]){""}说明属性对应表头的名称
index(int)-1说明属性在表格的位置
converter(Class<? extends Converter>)AutoConverter.class自定义转换类
@DateTimeFormat属性value(String)""设置转化格式
use1904windowing(boolean)false是否使用1904年日期系统

@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;
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值