1.easypoi,设计目的就是让没怎么学过poi的我们这种新手能够写更少的代码来快速地实现导入导出等功能。
2.eayspoi的官网https://easypoi.mydoc.io/
3.pom.xml要添加依赖
<!--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>
4.application文件
5.要导出的实体类,加上相应注解
package com.yl.demo.entity;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import java.io.Serializable;
import java.util.Date;
@ExcelTarget("student")
public class Student implements Serializable {
@Excel(name = "id")
private Integer id;
@Excel(name = "姓名")
private String name;
@Excel(name = "性别",replace = { "男_0", "女_1",})
private Integer sex;
@Excel(name = "出生日期", format = "yyyy-MM-dd", width = 20)
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 Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
}
6.编写controller协助测试
package com.yl.demo.controller;
import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.yl.demo.entity.Student;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 简单的excel导出
*/
@RestController
@RequestMapping("/singleExcel")
public class EasyPoiSingleExcelExportController {
/**
* 使用view,有可能有些浏览器不行
* @param ids
* @param map
* @return
*/
@GetMapping("/download")
public String downloadSingleExcel(@RequestParam List<Integer> ids, ModelMap map) {
//假设从数据库获取了学生列表信息
List<Student> list = getStudentList(ids);
//新建一个导出参数
ExportParams exportParams = new ExportParams("学生列表信息","学生列表信息", ExcelType.XSSF);
exportParams.setFreezeCol(2);
//指定导出的实体类
map.put(NormalExcelConstants.CLASS,Student.class);
//要导出的list集合
map.put(NormalExcelConstants.DATA_LIST,list);
map.put(NormalExcelConstants.PARAMS,exportParams);
return NormalExcelConstants.EASYPOI_EXCEL_VIEW;
}
/**
* 当用view没效果时,用这种方法
* @param map
* @param request
* @param response
*/
@GetMapping("/downloadNoView")
public void downloadByPoiBaseView(ModelMap map, HttpServletRequest request,
HttpServletResponse response) {
//获取所有学生列表信息
List<Student> studentList = getStudentList(null);
//新建一个导出参数
ExportParams exportParams = new ExportParams("学生列表信息","学生列表信息", ExcelType.XSSF);
exportParams.setFreezeCol(2);
//指定导出的实体类
map.put(NormalExcelConstants.CLASS,Student.class);
//要导出的list集合
map.put(NormalExcelConstants.DATA_LIST,studentList);
//指定导出参数
map.put(NormalExcelConstants.PARAMS,exportParams);
//使用PoiBaseView的render方法渲染
PoiBaseView.render(map,request,response,NormalExcelConstants.EASYPOI_EXCEL_VIEW);
}
private static List<Student> getStudentList(List<Integer> ids) {
//这里模拟前端传过来的是一个id集合,然后根据id去数据库获取学生信息再返回
//假设这是数据库查所有的学生信息
List<Student> list = new ArrayList<>();
List<Student> allStudents = new ArrayList<>();
Student s1 = new Student();
s1.setId(1);
s1.setName("路飞");
s1.setSex(0);
s1.setBirthDay(new Date());
allStudents.add(s1);
Student s2 = new Student();
s2.setId(2);
s2.setName("索隆");
s2.setSex(0);
s2.setBirthDay(new Date());
allStudents.add(s2);
Student s3 = new Student();
s3.setId(3);
s3.setName("娜美");
s3.setSex(1);
s3.setBirthDay(new Date());
allStudents.add(s3);
Student s4 = new Student();
s4.setId(4);
s4.setName("罗宾");
s4.setSex(1);
s4.setBirthDay(new Date());
allStudents.add(s4);
Student s5 = new Student();
s5.setId(5);
s5.setName("乔巴");
s5.setSex(0);
s5.setBirthDay(new Date());
allStudents.add(s5);
//模拟前端传来的id集合,获取对应的学生列表
if (ids != null) {
allStudents.stream().forEach(entity -> {
ids.stream().forEach(id -> {
if (entity.getId().equals(id)) {
list.add(entity);
}
});
});
return list;
} else {
return allStudents;
}
}
}
7.测试结果,使用第二种方法,使用view这种方法遇到了点问题。打开浏览器,输入http://localhost:9091/demo/singleExcel/downloadNoView,打开文件