EasyPoi极大的简化了poi的代码,通过自定义注解的方式来进行导出Excel数据。
本项目是利用maven管理。
pom文件中EasyPoi的依赖为:
cn.afterturn
easypoi-base
3.0.1
cn.afterturn
easypoi-web
3.0.1
cn.afterturn
easypoi-annotation
3.0.1
**学生类**
package com.cenhua.dataimport.pojo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import java.util.Date;
@ExcelTarget("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 = "出生日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd", isImportField = "true_st", width = 20)
private Date birthday;
@Excel(name = "进校日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd")
private Date registrationDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
public Date getRegistrationDate() {
return registrationDate;
}
public void setRegistrationDate(Date registrationDate) {
this.registrationDate = registrationDate;
}
}
**老师类**
package com.cenhua.dataimport.pojo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
@ExcelTarget("teacherEntity")
public class TeacherEntity implements java.io.Serializable {
private String id;
/** name */
//课程与老师是一对一的 老师与学生是一对多 课程与学生是一对多
@Excel(name = "主讲老师_major,代课老师_absent", orderNum = "1",isImportField = "true_major,true_absent",needMerge = true)
private String name;
@Excel(name = "职称", orderNum = "1") //needMerge 是一对多的列才能进行合并单元格
//普通合并用mergeVertical
private String message;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public TeacherEntity(String id, String name, String message) {
this.id = id;
this.name = name;
this.message = message;
}
public TeacherEntity(){
}
}
**课程类**
package com.cenhua.dataimport.pojo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import java.util.List;
/**
* 牵扯类
* 学生类 StudentEntity
* 老师类 TeacherEntity
*/
/**
* 课程类
*/
@ExcelTarget("OneToTwoExcelPoi")
public class OneToTwoExcelPoi implements java.io.Serializable{
/** 主键 */
private String id;
/** 课程名称 */
@Excel(name = "课程名称", orderNum = "1", width = 25)
private String name;
@Excel(name="课程批次",orderNum = "5",needMerge = true,width = 25)//orderNum属性是用来标记这个属性是第几列 因为orderNum=5时,会出现课程批次这一列会到学生列后面 就是规定
private String a;
/** 老师主键 */
@ExcelEntity(id = "absent") //标记是不是导出excel 标记为实体类,一遍是一个内部属性类,标记是否继续穿透,可以自定义内部id
private TeacherEntity mathTeacher;
//一个课程名称对应多个学生
//对于一对多来说课程的所有属性都对应多个学生 并且不用合并needMerge属性
@ExcelCollection(name = "学生", orderNum = "4")// 一对多的集合注解,用以标记集合是否被数据以及集合的整体排序 orderNum 列的排序,支持name_id
private List students;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TeacherEntity getMathTeacher() {
return mathTeacher;
}
public void setMathTeacher(TeacherEntity mathTeacher) {
this.mathTeacher = mathTeacher;
}
public List getStudents() {
return students;
}
public void setStudents(List students) {
this.students = students;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public OneToTwoExcelPoi(String id, String name, String a, TeacherEntity mathTeacher, List students) {
this.id = id;
this.name = name;
this.a = a;
this.mathTeacher = mathTeacher;
this.students = students;
}
public OneToTwoExcelPoi() {
}
}
**控制层**
package com.cenhua.dataimport.controller;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cenhua.dataimport.pojo.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* 一对多数据导出
*/
@Controller
public class DifficutExcelPoi {
@RequestMapping(value="/difficult")
public void testExportExcel_1() throws Exception {
ExportParams params = new ExportParams("0328课程表", "日期:2016-03-28", "六年一班");
//自造数据部分
List list1 = new ArrayList();
//循环添加学生数据 学生数列
StudentEntity studentEntity = null;
for (int i = 0; i < 3; i++) {
studentEntity = new StudentEntity();
studentEntity.setName("测试学生00" + i);
studentEntity.setBirthday(new Date());
studentEntity.setRegistrationDate(new Date());
studentEntity.setSex((i % 2 == 0 ? 1 : 2));
list1.add(studentEntity);
}
//老师数据 对象添加数据 对象一
TeacherEntity teacherEntity = new TeacherEntity();
teacherEntity.setId("101");
teacherEntity.setName("老王");
teacherEntity.setMessage("初级教师");
// 对象二
TeacherEntity teacherEntity1 = new TeacherEntity();
teacherEntity1.setId("102");
teacherEntity1.setName("老李");
teacherEntity1.setMessage("特高级教师");
//课程数据 课程对象一
List list = new ArrayList();
OneToTwoExcelPoi courseEntity = new OneToTwoExcelPoi();
courseEntity.setId("1001");
courseEntity.setName("计算机组成原理");
courseEntity.setA("子辰");
courseEntity.setStudents(list1);
courseEntity.setMathTeacher(teacherEntity);
list.add(courseEntity);
// 课程对象二
OneToTwoExcelPoi courseEntity1 = new OneToTwoExcelPoi();
courseEntity1.setId("1001");
courseEntity1.setName("Oracle数据库");
courseEntity1.setA("子辰");
courseEntity1.setStudents(list1);
courseEntity1.setMathTeacher(teacherEntity1);
list.add(courseEntity1);
Workbook workbook = ExcelExportUtil.exportExcel(params, OneToTwoExcelPoi.class,list);//courselist
FileOutputStream fos = new FileOutputStream("D:/excel/0328课程表.xls");
workbook.write(fos);
fos.close();
}
}
@ExcelTarget(“StudentEntity”):做一些通用的设置,可以限制行高(height)、设置文字大小(fontSize),"StudentEntity"就是设置的ID.
@Excel():这个是必须使用的注解,如果需求简单只使用这一个注解也是可以的,涵盖了常用的Excel需求,需要大家熟悉这个功能,主要分为基础,图片处理,时间处理,合并处理几块。
主要属性:name数据类型为String 设置的是列名、needMerge数据类型为boolean 是用来列合并的功能、orderNum数据类型为String 代表列的序号、width数据类型为double 设置列宽、mergeVertical数据类型为boolean 纵向合并内容相同的单元格。
@ExcelCollection():一对多的集合注解,用以标记集合是否被数据以及集合的整体排序。
@ExcelEntity():标记是不是导出excel 标记为实体类,一遍是一个内部属性类,标记是否继续穿透,可以自定义内部id。
http://easypoi.mydoc.io/#text_197817 此处为EasyPoi 1.0 的官方文档.