1.map形式的导出和简单的导出差不多,只是将每一行数据put到一个map里再装到一个list里,所需依赖
<!--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 com.yl.demo.entity;
import java.io.Serializable;
import java.util.Date;
/**
* 教师实体类
*/
public class Teacher implements Serializable {
private Integer id;
private String name;
private Integer sex;
private String course;
private Date birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
3.编写controller
package com.yl.demo.controller;
import cn.afterturn.easypoi.entity.vo.MapExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.yl.demo.entity.Student;
import com.yl.demo.entity.Teacher;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* map导出
*/
@RestController
@RequestMapping("/mapExport")
public class EasypoiMapExcelExportController {
@GetMapping("/download")
public void download(ModelMap modelMap, HttpServletRequest request,
HttpServletResponse response) {
List<ExcelExportEntity> entity = new ArrayList<>();
//每一个ExcelExportEntity代表着excel表格表头的那一个个字段
ExcelExportEntity excelentity1 = new ExcelExportEntity("id", "id");
entity.add(excelentity1);
ExcelExportEntity excelentity2 = new ExcelExportEntity("姓名", "name");
entity.add(excelentity2);
ExcelExportEntity excelentity3 = new ExcelExportEntity("性别", "sex");
entity.add(excelentity3);
ExcelExportEntity excelentity4 = new ExcelExportEntity("课程", "course");
entity.add(excelentity4);
ExcelExportEntity excelentity5 = new ExcelExportEntity("生日", "birthday");
entity.add(excelentity5);
//构建一个List存储map
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//模拟从数据库查数据
List<Teacher> teachers = getTeacherList();
teachers.stream().forEach(item -> {
//put的name对应上面创建的ExcelExportEntity的key
//tmap相当于excel表格的一行内容
Map<String,Object> tmap = new HashMap<>();
tmap.put("id",item.getId());
tmap.put("name",item.getName());
tmap.put("sex",item.getSex() == 1 ? "女":"男");
tmap.put("course",item.getCourse());
tmap.put("birthday",sdf.format(item.getBirthday()));
//讲数据存到List中
list.add(tmap);
});
//创建导出参数
ExportParams params = new ExportParams("老师信息列表", "老师信息列表", ExcelType.XSSF);
params.setFreezeCol(2);
//指定要导出的List集合数据
modelMap.put(MapExcelConstants.MAP_LIST, list);
//指定ExcelExportEntity
modelMap.put(MapExcelConstants.ENTITY_LIST, entity);
//指定要导出的参数
modelMap.put(MapExcelConstants.PARAMS, params);
//文件名
modelMap.put(MapExcelConstants.FILE_NAME, "老师信息列表");
PoiBaseView.render(modelMap, request, response, MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW);
}
private static List<Teacher> getTeacherList() {
//这里模拟去数据库获取老师信息再返回
List<Teacher> allTeachers = new ArrayList<>();
Teacher t1 = new Teacher();
t1.setId(1);
t1.setName("赵无极");
t1.setSex(0);
t1.setCourse("语文");
t1.setBirthday(new Date());
allTeachers.add(t1);
Teacher t2 = new Teacher();
t2.setId(2);
t2.setName("马红俊");
t2.setSex(0);
t2.setCourse("数学");
t2.setBirthday(new Date());
allTeachers.add(t2);
Teacher t3 = new Teacher();
t3.setId(3);
t3.setName("小舞");
t3.setSex(1);
t3.setCourse("音乐");
t3.setBirthday(new Date());
allTeachers.add(t3);
Teacher t4 = new Teacher();
t4.setId(4);
t4.setName("宁荣荣");
t4.setSex(1);
t4.setCourse("地理");
t4.setBirthday(new Date());
allTeachers.add(t4);
Teacher t5 = new Teacher();
t5.setId(5);
t5.setName("花海");
t5.setSex(0);
t5.setCourse("化学");
t5.setBirthday(new Date());
allTeachers.add(t5);
//获取老师列表
return allTeachers;
}
}
4.测试,在浏览器输入http://localhost:9091/demo/mapExport/download,结果