仅记录,非教程
Easyexcel官网:Easyexcel
导出数据即在Excel中写入数据
引入插件
在pom.xml引入插件
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>4.0.2</version>
</dependency>
实体类
编写实体类entity,对应要导出的数据字段
@EqualsAndHashCode
public class ExportData
{
@ExcelProperty("地址")
private Integer ip;
@ExcelProperty("编号")
private Integer id;
@ExcelProperty("标识")
private Integer flag;
@ExcelProperty("重量")
private Integer weight;
@ExcelProperty("时间")
@ColumnWidth(20)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date createdate;
}
效果
@EqualsAndHashCode 注解用于自动生成 equals 和 hashCode 方法。这两个方法在 Java 中用于比较对象的等价性和获取对象的HashCode
@ExcelProperty用于匹配实体类和Excel中的字段,使其一一对应,
其余注解可以在官方文档获取相关信息和使用方法
写入基本用法
示例:
public void ExportData(List<Weight> weights){
if (weights!=null)
{
try{
String fileName="ExportData"+ System.currentTimeMillis()+".xlsx";
EasyExcel.write(fileName, ExportData.class).sheet("数据").doWrite(weights);
}catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
这里将要处理的数据进行写入填充,write方法的参数分别是文件名和要写入的实体类即entity