1引入依赖
<!--easypoi的依赖-->
<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 *;
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
import java.util.List;
@Data
public class Member {
//姓名
@Excel(name = "REAL_NAME")
private String name;
//电话
@Excel(name = "MOBILE")
private String phone;
//邮件
@Excel(name = "EMAIL")
private String email;
//邮件
private String selfEmail;
//账号
@Excel(name = "PRACCT_NAME")
private String fourUserName;
//新增默认状态
private String useStatus="1";
//接收部门
@Excel(name = "ORG_NAME")
private String orgNAME;
private String wbRoleId;
}
3 业务代码
public Result membersBulkLoad(MultipartFile file) throws Exception {
// 1.解析文档需要用到ImportParams 对象
final ImportParams importParams = new ImportParams();
importParams.setHeadRows(1);
// 2.使用ExcelImportUtil工具类获取Excel表格中的内容,并转为List<T>,实际情况下我们需要知道上传的表格是什么类型的
// *.因为所有的pojo都是按照用户需求创建的,拥有模板,上传的数据必须符合这些模板
// *.那么实际开发中,可以通过访问的url判断本次上传的文件是属于什么类型的pojo
final List<Member> students = ExcelImportUtil.importExcel(
file.getInputStream(),
Member.class,
importParams
);
// 3.写入数据库,其中如果数据格式不符合,在上一步转换的时候会报错
// this.studentService.saveAll(students);
System.out.println(students);
return Result.success(students);
}
4excel文档,字段名要与实体类对应
个人感觉这个真的比poi好用,poi接收时候数字就是数字,字符串就是字符串类型,这个即使字符串有数字也可以用字符串接收。