概况
今天做Excel导出时,发现了一款非常好用的POI框架EasyPoi,其 使用起来简洁明了。现在我们就来介绍下EasyPoi,首先感谢EasyPoi 的开发者 Lemur开源
easypoi 简介
easypoi 是为了让开发者快速的实现excel,word,pdf的导入导出,基于Apache poi基础上的一个工具包。
特性
基于注解的导入导出,修改注解就可以修改Excel
支持常用的样式自定义
基于map可以灵活定义的表头字段
支持一对多的导出,导入
支持模板的导出,一些常见的标签,自定义标签
支持HTML/Excel转换
支持word的导出,支持图片,Excel
常用注解
@Excel注解
@Excel 注解是作用到Filed 上面,是对Excel一列的一个描述,这个注解是必须要的注解,其部分属性如下:
其使用如下,其中orderNum是指定该字段在Excel中的位置,name与Excel中对应的表头单元格的名称
@Excel(name = "主讲老", orderNum = "1") private String name;
@ExcelCollection 注解
@ExcelCollection 注解表示一个集合,主要针对一对多的导出
比如一个老师对应多个科目,科目就可以用集合表示,作用在一个类型是List的属性上面,属性如下:
其使用如下所示。
@ExcelCollection(name = "学生", orderNum = "4") private List<StudentEntity> students;
@ExcelEntity注解
@ExcelEntity注解表示一个继续深入导出的实体,是作用一个类型为实体的属性上面,其属性如下:
其使用如下所示。
@ExcelEntity(id = "major") private TeacherEntity chineseTeacher;
@ExcelIgnore 注解
@ExcelIgnore 注解修饰的字段,表示在导出的时候补导出,被忽略。
@ExcelTarget 注解
@ExcelTarget注解作用于最外层的对象,描述这个对象的id,以便支持一个对象,可以针对不同导出做出不同处理,其作用在实体类的上,属性如下:
其使用如下:
@ExcelTarget("scoreIssueReqPOJO") public class ScoreIssueReqPOJO implements java.io.Serializable{}
EasyPOI的使用
1.引入依赖
1.SSM 项目,引入依赖
如果spring的版本是4.x的话引入的easypoi的版本是3.0.1
,如果spring是5.x的话引入easypoi的版本是4.0.0
<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>4.0.0</version> </dependency>
2.Spring Boot 项目(2.x以上的版本,我demo的版本是2.1.3.RELEASE),引入依赖
<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>4.0.0</version> </dependency>
需要注意的是由于easypoi的依赖内部依赖原生的poi,所以,引入了easypoi的依赖之后,需要把原生的poi的依赖删掉
注解方式导出Excel
导出测试的demo
@Test public void testExportExcel() throws Exception { List<CourseEntity> courseEntityList = new ArrayList<>(); CourseEntity courseEntity = new CourseEntity(); courseEntity.setId("1"); courseEntity.setName("测试课程"); TeacherEntity teacherEntity = new TeacherEntity(); teacherEntity.setName("张老师"); teacherEntity.setSex(1); courseEntity.setMathTeacher(teacherEntity); List<StudentEntity> studentEntities = new ArrayList<>(); for (int i = 1; i <= 2; i++) { StudentEntity studentEntity = new StudentEntity(); studentEntity.setName("学生" + i); studentEntity.setSex(i); studentEntity.setBirthday(new Date()); studentEntities.add(studentEntity); } courseEntity.setStudents(studentEntities); courseEntityList.add(courseEntity); Date start = new Date(); Workbook workbook = ExcelExportUtil.exportExcel( new ExportParams("导出测试", null, "测试"), CourseEntity.class, courseEntityList); System.out.println(new Date().getTime() - start.getTime()); File savefile = new File("D:/excel/"); if (!savefile.exists()) { savefile.mkdirs(); } FileOutputStream fos = new FileOutputStream("D:/excel/教师课程学生导出测试.xls"); workbook.write(fos); fos.close(); }
导出对应的Bean
CourseEntity 类。
@ExcelTarget("courseEntity") public class CourseEntity implements java.io.Serializable { /** 主键 */ private String id; /** 课程名称 */ @Excel(name = "课程名称", orderNum = "1", width = 25,needMerge = true) private String name; /** 老师主键 */ //@ExcelEntity(id = "major") private TeacherEntity chineseTeacher; /** 老师主键 */ @ExcelEntity(id = "absent") private TeacherEntity mathTeacher; @ExcelCollection(name = "学生", orderNum = "4") private List<StudentEntity> students;
TeacherEntity 类
@Data public class TeacherEntity { /** * 学生姓名 */ @Excel(name = "教师姓名", height = 20, width = 30, isImportField = "true_st") private String name; /** * 学生性别 */ @Excel(name = "教师性别", replace = {"男_1", "女_2"}, suffix = "生", isImportField = "true_st") private int sex; }
StudentEntity 类。
public class StudentEntity implements java.io.Serializable { /** * id */ private String id; /** * 学生姓名 */ @Excel(name = "学生姓名", height = 20, width = 30, isImportField = "true_st") private String name; /** * 学生性别 */ @Excel(name = "学生性别", replace = { "男_1", "女_2" }, suffix = "生", isImportField = "true_st") private int sex; @Excel(name = "出生日期", exportFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd", isImportField = "true_st", width = 20) private Date birthday; @Excel(name = "进校日期", exportFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd") private Date registrationDate;
导出结果
关于日期格式化的说明
如果是导出的实体类(就是说这个实体类是对应导出的Excel的),那么用@Excel注解的exportFormat属性来格式化日期。如下所示:
@Excel(name = "出生日期", exportFormat = "yyyy-MM-dd HH:mm:ss", width = 20)
如果是导入的实体类(就是说这个实体类是对应导入的Excel的),那么用@Excel注解的importFormat属性来格式化日期。如下所示:
@Excel(name = "添加时间",importFormat = "yyyy-MM-dd HH:mm:ss",orderNum = "14") private Date createTime;
@Excel
注解的databaseFormat 属性是用于数据库的格式不是日期类型,如datetime时用。