easyExcel使用教程–导出篇
开始准备工作
1、导入Maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
<version>4.12</version>
</dependency>
2、新建Student.java类
package com.test.easyexcel.bean;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.math.BigDecimal;
import java.util.Date;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor // 一定要有无参构造方法
public class Student {
@ExcelProperty(value = "姓名")
private String name;
@ExcelProperty(value = "性别")
private Integer sex;
@ExcelProperty(value = "生日")
private Date birthday;
@ExcelProperty(value = "体重KG")
private BigDecimal weight;
private String memo;
}
3、generateStudentUtil.java类,随机生成Student对象
package com.test.easyexcel.util;
import com.test.easyexcel.bean.Student;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class generateStudentUtil {
public static List<Student> generateStudent(int size) {
List<Student> stues = new ArrayList<>();