前言:easypoi教程请参考http://easypoi.mydoc.io/#text_231892
员工实体类
package cn.itsource.ibs.test.easypoi;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import java.util.Date;
@ExcelTarget("employee")//目标对象
public class POIEmployee {
@Excel(name = "编号")//该字段对应的表头设置
private Long id;
@Excel(name = "用户名")
private String username;
@Excel(name = "邮箱", width = 30)
private String email;
@Excel(name = "年龄")
private Integer age;
@Excel(name = "性别", replace = {"男_true", "女_false"})
private Boolean gender;
@Excel(name = "头像", type = 2, width = 5, height = 10, savePath = "img/upload/")
private String headImage;
@Excel(name = "生日", format = "yyyy-MM-dd", width = 20)
private Date birthday = new Date();
/**
* @ExcelEntity表示导出当前对象数据的时候还要顺便导出这个关联对象中加了@Excel注解的那些属性值
*/
@ExcelEntity
private POIDepartment department;
public POIEmployee() {
}
public POIEmployee(Long id, String username, String email, Integer age, Boolean gender, String headImage) {
this.id = id;
this.username = username;
this.email = email;
this.age = age;
this.gender = gender;
this.headImage = headImage;
}
public POIEmployee(Long id, String username, String email, Integer age, Boolean gender, String headImage, POIDepartment department) {
this.id = id;
this.username = username;
this.email = email;
this.age = age;
this.gender = gender;
this.headImage = headImage;
this.department = department;
}
public POIDepartment getDepartment() {
return department;
}
public void setDepartment(POIDepartment department) {
this.department = department;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getHeadImage() {
return headImage;
}
public void setHeadImage(String headImage) {
this.headImage = headImage;
}
public Boolean getGender() {
return gender;
}
public void setGender(Boolean gender) {
this.gender = gender;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "POIEmployee{" +
"id=" + id +
", username='" + username + '\'' +
", email='" + email + '\'' +
", age=" + age +
", gender=" + gender +
", headImage='" + headImage + '\'' +
", birthday=" + birthday +
", department=" + department +
'}';
}
}
部门实体类
package cn.itsource.ibs.test.easypoi;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
@ExcelTarget("department")//目标对象
public class POIDepartment {
@Excel(name = "部门编号_department")
private Long id;
@Excel(name = "所属部门_employee,部门名称_department")//表示如果是员工那边导出,则该字段的表头为所属部门,如果是部门这边导出,表头为部门名称
private String name;
public POIDepartment() {
}
public POIDepartment(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "POIDepartment{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
2.导出测试
/**
* 测试普通导出
*
* @throws Exception
*/
@Test
public void testExport01() throws Exception {
List list = new ArrayList();
list.add(new POIEmployee(1L, "admin1", "[email protected]", 20, null, null));
list.add(new POIEmployee(2L, "admin2", "[email protected]", 21, null, null));
list.add(new POIEmployee(3L, "admin3", "[email protected]", 22, null, null));
list.add(new POIEmployee(4L, "admin4", "[email protected]", 23, null, null));
list.add(new POIEmployee(5L, "admin5", "[email protected]", 24, null, null));
/**
* easypoi导出数据到excel
* 第一个参数:ExportParams entity, 导出参数[包含导出的表格标题以及sheet名称]
* 第二个参数:Class> pojoClass POIEmployee.class 导出数据domain实体类的字节码对象
* 第三个参数:Collection> dataSet 导出的数据集合
*/
Workbook workbook = ExcelExportUtil.exportExcel(
new ExportParams("员工信息列表", "员工信息"),
POIEmployee.class,
list);
//输出流输出数据
OutputStream out = new FileOutputStream("员工信息列表.xls");
workbook.write(out);
out.flush();
out.close();
}
/**
* 测试性别字段值替换导出
*
* @throws Exception
*/
@Test
public void testExport02() throws Exception {
List list = new ArrayList();
list.add(new POIEmployee(1L, "admin1", "[email protected]", 20, true, null));
list.add(new POIEmployee(2L, "admin2", "[email protected]", 21, true, null));
list.add(new POIEmployee(3L, "admin3", "[email protected]", 22, false, null));
list.add(new POIEmployee(4L, "admin4", "[email protected]", 23, false, null));
list.add(new POIEmployee(5L, "admin5", "[email protected]", 24, true, null));
Workbook workbook = ExcelExportUtil.exportExcel(
new ExportParams("员工信息列表", "员工信息"),
POIEmployee.class,
list);
//输出流输出数据
OutputStream out = new FileOutputStream("员工信息列表.xls");
workbook.write(out);
out.flush();
out.close();
}
/**
* 测试头像、日期导出
*
* @throws Exception
*/
@Test
public void testExport03() throws Exception {
List list = new ArrayList();
list.add(new POIEmployee(1L, "admin1", "[email protected]", 20, true, "img/head001.jpg"));
list.add(new POIEmployee(2L, "admin2", "[email protected]", 21, true, "img/head001.jpg"));
list.add(new POIEmployee(3L, "admin3", "[email protected]", 22, false, "img/head002.jpg"));
list.add(new POIEmployee(4L, "admin4", "[email protected]", 23, false, "img/head001.jpg"));
list.add(new POIEmployee(5L, "admin5", "[email protected]", 24, true, "img/head002.jpg"));
Workbook workbook = ExcelExportUtil.exportExcel(
new ExportParams("员工信息列表", "员工信息"),
POIEmployee.class,
list);
//输出流输出数据
OutputStream out = new FileOutputStream("员工信息列表.xls");
workbook.write(out);
out.flush();
out.close();
}
/**
* 测试导出员工关联的部门信息
*
* @throws Exception
*/
@Test
public void testExport04() throws Exception {
POIDepartment department01 = new POIDepartment(1L, "销售部");
POIDepartment department02 = new POIDepartment(2L, "IT部");
POIDepartment department03 = new POIDepartment(3L, "公关部");
POIDepartment department04 = new POIDepartment(4L, "人事部");
List list = new ArrayList();
list.add(new POIEmployee(1L, "admin1", "[email protected]", 20, true, "img/head001.jpg", department01));
list.add(new POIEmployee(2L, "admin2", "[email protected]", 21, true, "img/head001.jpg", department03));
list.add(new POIEmployee(3L, "admin3", "[email protected]", 22, false, "img/head002.jpg", department04));
list.add(new POIEmployee(4L, "admin4", "[email protected]", 23, false, "img/head001.jpg", department02));
list.add(new POIEmployee(5L, "admin5", "[email protected]", 24, true, "img/head002.jpg", department01));
Workbook workbook = ExcelExportUtil.exportExcel(
new ExportParams("员工信息列表", "员工信息"),
POIEmployee.class,
list);
//输出流输出数据
OutputStream out = new FileOutputStream("员工信息列表.xls");
workbook.write(out);
out.flush();
out.close();
}
/**
* 测试只导出部门信息
*
* @throws Exception
*/
@Test
public void testExport05() throws Exception {
POIDepartment department01 = new POIDepartment(1L, "销售部");
POIDepartment department02 = new POIDepartment(2L, "IT部");
POIDepartment department03 = new POIDepartment(3L, "公关部");
POIDepartment department04 = new POIDepartment(4L, "人事部");
List list = new ArrayList();
list.add(department01);
list.add(department02);
list.add(department03);
list.add(department04);
Workbook workbook = ExcelExportUtil.exportExcel(
new ExportParams("部门信息列表", "部门信息"),
POIDepartment.class,
list);
//输出流输出
OutputStream out = new FileOutputStream("部门信息列表.xls");
workbook.write(out);
out.flush();
out.close();
}
/**
* 导入测试
*
* @throws Exception
*/
@Test
public void testImport() throws Exception {
//导入参数
ImportParams params = new ImportParams();
params.setTitleRows(1); //导入数据的时候排除标题行
params.setHeadRows(1); //导入数据的时候排除表头行
FileInputStream in = new FileInputStream("员工信息列表.xls");
List list = ExcelImportUtil.importExcel(
in,
POIEmployee.class, params);
list.forEach(e -> System.out.println(e));
}