【无标题】

本文详细介绍了EasyExcel的注解使用,包括@ExcelIgnore、@ExcelProperty、@DateTimeFormat和@NumberFormat等,以及它们在数据导出过程中的作用。示例代码展示了如何使用EasyExcel导出包含学生信息的Excel文件,同时解释了导出数据的过程,从行创建到单元格数据转换的步骤。

具体使用示例链接:语雀 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 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()方法调用

https://www.cnblogs.com/gne-hwz/p/14479634.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值