一、Easypoi 注解类
1. @ExcelTarget
-
作用:标记一个实体类,表示该类是一个 Excel 导入导出的目标实体。
-
常用属性:
-
value
:标识符,用于在嵌套实体中区分不同的实体类。
-
// 示例
@ExcelTarget("student")
public class Student {
// 字段定义
}
2. @Excel
用于标记实体类中的字段与 Excel 列的映射关系。(用在实体类的列字段注解)
3. @ExcelEntity
-
作用:标记一个字段(实体类中的字段),表示该字段是一个嵌套实体类。
-
常用属性:
-
name
:嵌套实体在 Excel 中的表头名称。 -
show
:是否显示该嵌套实体(默认true
)。 -
id
:与@ExcelTarget
的value
对应,用于关联嵌套实体。
-
// 示例
@ExcelTarget(value = "student")
public class Student{
// 字段a/b/c......
@ExcelEntity(name = "班级信息")
private ClassInfo classInfo;
}
4. @ExcelCollection
用于标记嵌套的集合类型字段,支持复杂表头。(用于实体类中列表字段注解)
常用属性:
-
name
:集合列的名称(表头)。 -
orderNum
:列的顺序。 -
type
:集合的类型(默认List.class
)。
// 示例
@ExcelCollection(name = "课程信息", orderNum = "3")
private List<Course> courses;
5. @ExcelIgnore
用于标记不需要导入导出的字段。(注解用在实体类的列字段注解)
二、Easypoi 工具类
1. ExcelExportUtil
用于导出 Excel 文件。
常用方法:
-
exportExcel(ExportParams params, Class<?> pojoClass, Collection<?> dataSet)
:导出 Excel。-
params
:导出参数(如标题、表头等)。 -
pojoClass
:实体类的 Class 对象。 -
dataSet
:数据集合。
-
ExportParams exportParams = new ExportParams("学生信息表", "学生信息");
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Student.class, students);
2. ExcelImportUtil
用于导入 Excel 文件。
常用方法:
-
importExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params)
:导入 Excel。-
inputstream
:Excel 文件的输入流。 -
pojoClass
:实体类的 Class 对象。 -
params
:导入参数(如标题行、表头行等)。
-
ImportParams importParams = new ImportParams();
importParams.setTitleRows(1); // 标题行数
importParams.setHeadRows(2); // 表头行数
List<Student> students = ExcelImportUtil.importExcel(file.getInputStream(), Student.class, importParams);
3. ExportParams
用于配置导出参数。
常用属性:
-
title
:Excel 的标题。 -
sheetName
:工作表的名称。 -
type
:导出类型(如HSSF
或XSSF
)。 -
isCreateHeadRows
:是否创建表头(默认true
)。
ExportParams exportParams = new ExportParams("学生信息表", "学生信息");
exportParams.setType(ExcelType.XSSF); // 导出为 .xlsx 文件
4. ImportParams
用于配置导入参数。
常用属性:
-
titleRows
:标题行数。 -
headRows
:表头行数。 -
startSheetIndex
:开始读取的工作表索引。 -
keyIndex
:主键列索引(用于去重)。 -
needVerify
:是否需要校验数据(默认false
)。
ImportParams importParams = new ImportParams();
importParams.setTitleRows(1);
importParams.setHeadRows(2);
5. PoiPublicUtil
提供了一些公共工具方法。
常用方法:
-
getWebRootPath()
:获取 Web 根路径。 -
getFileByUrl(String url)
:根据 URL 获取文件。
三、完整示例
1、导入包
<!--Stringboot 集成easypoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
2、实体类
@ExcelTarget(value = "student") // 标记为Excel的实体目标
public class Student {
@Excel(name = "学生姓名", orderNum = "0")
private String name;
@Excel(name = "学生年龄", orderNum = "1")
private Integer age;
@ExcelCollection(name = "课程信息", orderNum = "2", id = "courses")
private List<Course> courses;
@ExcelEntity(name = "班级信息")
private ClassInfo classInfo;
// get/set/构造方法....
}
@ExcelTarget("courses") // 标记为 Excel 目标实体,id 与 @ExcelCollection 的 id 对应
public class Course {
@Excel(name = "课程名称", orderNum = "0")
private String courseName;
@Excel(name = "课程成绩", orderNum = "1")
private Integer score;
public String getCourseName() {
return courseName;
}
// get/set/构造方法....
}
public class ClassInfo {
@Excel(name = "班级名称", orderNum = "0")
private String className;
@Excel(name = "班主任", orderNum = "1")
private String teacherName;
}
@RestController
@RequestMapping(value = "/excel")
public class ExcelController {
@GetMapping("/export")
public void exportExcel(HttpServletResponse response) throws IOException, IOException {
List<Student> students = new ArrayList<>();
// 模拟数据
List<Course> courses = new ArrayList<>();
courses.add(new Course("数学", 90));
courses.add(new Course("英语", 85));
ClassInfo classInfo = new ClassInfo("高三一班", "张老师");
students.add(new Student("张三", 18, courses, classInfo));
students.add(new Student("李四", 19, courses, classInfo));
// 导出参数
ExportParams exportParams = new ExportParams("学生信息表", "学生信息");
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Student.class, students);
// 设置响应头
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=student.xlsx");
// 写入响应流
workbook.write(response.getOutputStream());
workbook.close();
}
@PostMapping("/import")
public List<Student> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
ImportParams importParams = new ImportParams();
importParams.setTitleRows(1); // 表头行数
importParams.setHeadRows(2); // 复杂表头行数
List<Student> studentList = ExcelImportUtil.importExcel(file.getInputStream(), Student.class, importParams);
return studentList;
}
说明:
在Easypoi中,复杂表头可以通过@ExcelCollection
和@ExcelEntity
注解来处理。@ExcelCollection
用于处理嵌套的集合类型数据,@ExcelEntity
用于处理嵌套的实体类型数据。