EasyExcel注解方式导出数据过程解析

具体使用示例链接:语雀 EasyExcel https://www.yuque.com/easyexcel/doc/write

示例代码

public class StudentExportDto {

    @ExcelProperty(value = {"学生信息", "姓名"} ,index = 0)
    private String name;

    @ExcelProperty(value = {"学生信息", "年龄"} ,index = 1)
    private Integer age;

    @DateTimeFormat(value="yyyy-MM")
    @ExcelProperty(value = {"学生信息", "出生年月"} ,index = 2)
    private Date birthday;

    @NumberFormat(value="#.00")
    @ExcelProperty(value = {"学生信息", "数学分数"} ,index = 3)
    private Double mathScore;

    @NumberFormat(value="#.00")
    @ExcelProperty(value = {"学生信息", "语文分数"} ,index = 4)
    private Double chineseScore;

    @NumberFormat(value="#.00")
    @ExcelProperty(value = {"学生信息", "平均分数"} ,index = 5)
    private Double averageScore;
}

测试

public void test() throws FileNotFoundException {
        OutputStream out = new FileOutputStream("E:\\2007.xlsx");
        try {
            List<StudentExportDto> listData = new ArrayList<>();
            StudentExportDto dto = new StudentExportDto("张三", 13, new Date(),
                    56.5, (double) 98, (56.5+98)/2);
            listData.add(dto);
            ExcelWriter writer = EasyExcel.write(out, StudentExportDto.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .build();
            WriteSheet writeSheet = EasyExcel.writerSheet("学生信息").build();
            writer.write(listData, writeSheet);
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

 

EasyExcel的注解含义解释

@ExcelIgnore

这个注解标记在类属性上,属性会在导出时被忽略

@ExcelIgnoreUnannotated

标记在类上,见名知意,
在解析类的所有easyExcel属性注解时,忽略没有注解的类
即没有注解的属性被忽略导出,这个注解不常用,直接用@ExcelIgnore注解就可以了

@ExcelProperty

这个是用来初始化属性列的配置,
比如配置类型转换器Converter,配置列的标题
列对应的index排序大小
@ExcelProperty(value="列标题", index=1, converter=XXConverter.class)

@NumberFormat

这个数值格式化注解时内置转换器使用,当没有配置自定义转换器converter时
会根据field.getType类型+ String匹配内置的转换器。如 DoubleStringConverter

@ExcelProperty没有设置converter
而属性上有@NumberFormat注解,默认数值类型会被转换成字符串,
默认操作是会添加一个默认转换器DoubleStringConverter

@DateTimeFormat

这个注解时内置转换器使用
DateNumberConverter: 这个是当属性类型为Date,excel中类型为CellDataTypeEnum.NUMBER时会调用这个转换器
DateStringConverter:这个也是,当excel中类型为CellDataTypeEnum.STRING时调用这个

 

下面这些是用来设置excel表样式用了
@ColumnWidth
@ContentFontStyle
@ContentLoopMerge
@ContentRowHeight
@ContentStyle
@HeadFontStyle
@HeadRowHeight
@HeadStyle
@OnceAbsoluteMerge

 

注解导出数据过程

easyexcel注解导出excel数据
beforeRowCreate    行数据创建之前
    RowWriteHandler.beforeRowCreate()方法调用
createRow    行row创建
afterRowCreate 行数据创建之后
    RowWriteHandler.afterRowCreate()方法调用

写入List数据或者其他JavaObject数据
        CellWriteHandler.beforeCellCreate() 创建Cell前
        创建行
        CellWriteHandler.afterCellCreate() 创建cell后
        调用converterAndSet进行类型转换
            在这里转换时,如果这一行数据是List类型对象,则做基础数据转成excel类型
            如果一行数据是其他Object对象,将对象转成map,
            然后通过@ExcelProperty注解配置的Converter,DateTimeFormatProperty和NumberFormatProperty
            将对应Field属性值进行转换和格式化生成excel的CellData数据设置到单元格中
        CellWriteHandler.afterCellDataConverted() 数据格式转换后
        设置到单元格
    
afterRowDispose    行数据写入row关闭后
    RowWriteHandler.afterRowDispose()方法调用

 

posted @ 2021-03-04 14:03  海绵般汲取  阅读(4399)  评论(0编辑  收藏  举报