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()方法調用

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM