使用alibaba提供的easyexcel方法导出数据到excel文档
文章目录
前言
该方式适用于前端页面点击导出然后将数据导出成excel文件的场景
依据导出数据实体类来自动导出我们想要的字段数据
一、EasyExcel 是什么?
EasyExcel 是一个基于 Java 的简单、省内存的读写 Excel 的开源项目。
在尽可能节约内存的情况下支持读写百 M 的 Excel(没有一次性将数据读取到内存中,从磁盘中一行行读取,从而节约内存)。
EasyExcel 采用一行一行的解析模式,并将一行的解析结果以观察者的模式通知处理(AnalysisEventListener)。
简单地理解:可以方便、快速的读取、写入 execl 内容。
【官方文档地址:】
https://alibaba-easyexcel.github.io/index.html
https://www.yuque.com/easyexcel
二、使用步骤
1.引入库
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
2.输出的实体类(表头)
@Data
public class Resource {
@ExcelProperty(value = "id", index = 0)
private Integer id;
@ExcelProperty(value = "资源名称", index = 1)
private long name;
@ExcelProperty(value = "资源时间", index = 2)
private String displayTime;
@ExcelProperty(value = "总量", index = 3)
private Integer count;
/**
* 打印数据为:1 或 2 需要转换一下。
* 1-需要
* 2-不需要
*/
@ExcelProperty(value = "是否需要打印", index = 4,converter =StatusConverter.class)
private String collectStatus;
/**
* 该字段无需导出
*/
@ExcelIgnore
private String orgCode;
}
3.字段转义方法CollectStatusConverter(按需使用)
public class CollectStatusConverter implements Converter<String> {
@Override
public Class<String> supportJavaTypeKey() {
return String.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum