对EasyExcel的常用注解的详细介绍

通过下表简单介绍一些使用过的注解,内容并不全面

注解

说明

@ColumnWidth(15) 控制行的宽度,用在class上代表所有单元格的宽度,用在属性上代表当前列的宽度
@ContentRowHeight(25) 控制内容的高度,用在class上代表所有单元格的宽度,用在属性上代表当前列的宽度
@HeadRowHeight(20) 控制标题的高度
@ExcelIgnore 导出时忽略该属性,默认是导出全部的属性,无论属性上写没写@ExcelProperty
@ExcelIgnoreUnannotated 在类上使用该注解,忽略所有未添加@ExcelProperty的属性

注解 相关子属性 说明
@ContentStyle 设置导出时的单元格格式,可用在class上也可用在属性上
horizontalAlignment 使用HorizontalAlignmentEnum.CENTER时,意味着水平居中
verticalAlignment 使用VerticalAlignmentEnum.CENTER时,意味着垂直居中
indent 缩进
locked 是否冻结
hidden 是否隐藏
dataFormat 数据格式
rotation 旋转角度,用于图片
quotePrefix 是否引用前缀(没用过)
wrapped

控制数据的包装是否直接应用,默认为null,不应用,

例如单元格内换行,如果改值不设置为true,需要双击单元格才能换行

@ContentFontStyle 设置导出时的文本格式
underline 下划线,具体值可以参考org.apache.poi.ss.usermodel.FontUnderline的值,但不能直接引用
color 颜色,具体值可以参考org.apache.poi.ss.usermodel.IndexedColors的值,但不能直接引用
bold 是否加粗
strikeout 是否加删除线
fontName 字体类型(宋体、微软雅黑...)
italic 是否斜体
fontHeightInPoints 字体高度
@ExcelProperty 标识导出或导入时,该属性为有效列
value 代表Excel的首行标题名称,标题名称相同且相邻的话,标题会自动合并
order 排序,数值越小越靠前
index 指定列号
converter 在遇到复杂数据时,可自定义处理工具,然后在该属性的位置加入自定义处理工具XxxxConverter.class自定义处理工具需要实现接口Converter<T>
@DateTimeFormat
value 时间类型转为字符串格式。值可为:yyyy-MM-dd HH:mm:ss

简单举例自定义处理接口

@ExcelProperty(value = "自定义标题名", order = 1, converter = CustomStringConverter.class)

private String customTitle;


public class CustomStringConverter implements Converter<String> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 这里读的时候会调用
     *
     * @param context
     * @return
     */
    @Override
    public String convertToJavaData(ReadConverterContext<?> context) {
        return "自定义:" + context.getReadCellData().getStringValue();
    }

    /**
     * 这里写的时候会调用
     * @return
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {
        String stringValue = context.getValue();
        if (stringValue==null || stringValue.isEmpty()){
            stringValue = "这是一个空值";
        }
        return new WriteCellData<>(stringValue);
    }

}

详情请参考官方文档:关于Easyexcel | Easy Excel

猜你喜欢

转载自blog.csdn.net/cccsssrrr/article/details/127813042