1.依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
2.实体类
package com.yl.jasperreportdemo.domain;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import java.io.Serializable;
import java.util.Date;
@ExcelTarget("student")
public class Student implements Serializable {
@Excel(name = "name")
private String name;
@Excel(name = "age")
private int age;
@Excel(name = "sex",replace = { "男_0", "女_1",})
private int sex;
@Excel(name = "date",format = "yyyy-MM-dd")
private Date birthDay;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", birthDay=" + birthDay +
'}';
}
}
3.准备好要导入的excel数据,路径为F:/test1.xlsx
4.测试
package com.yl.jasperreportdemo.controller;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.yl.jasperreportdemo.domain.Student;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.util.List;
public class ImportController {
public static void main(String[] args) {
ImportParams params = new ImportParams();
params.setTitleRows(0);
params.setHeadRows(1);
List<Student> list = ExcelImportUtil.importExcel(
new File("F:/test1.xlsx"),
Student.class, params);
System.out.println(list.size());
if (!CollectionUtils.isEmpty(list)) {
list.stream().forEach(item -> {
System.out.println(item);
});
}
}
}
5.结果