官方文檔
點我直達
添加依賴
這裡SpringBoot項目就不帶領大家建立了,直接在pom.xml中添加esayexcel依賴即可
<!--easyexcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
建立導出實體類
Student.java
package net.ybclass.online_ybclass.exceldemo;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
/**
* @ClassName:Student
* @Description:Excel導出實體類
* @Author:chenyb
* @Date:2020/11/11 10:30 上午
* @Versiion:1.0
*/
@ExcelIgnoreUnannotated()
public class Student {
private Integer myId;
@ExcelProperty(value = "學号",index = 0) //0 對應導出Excel表格的第一列
private Integer id;
@ExcelProperty(value = "姓名",index = 1)
private String name;
@ExcelProperty(value = "性别",index = 2)
private String sex;
@ExcelProperty(value = "班級",index = 3)
private String grade;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", grade='" + grade + '\'' +
'}';
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
控制層
package net.ybclass.online_ybclass.exceldemo;
import com.alibaba.excel.EasyExcel;
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.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @ClassName:ExcelController
* @Description:excel導出控制層
* @Author:chenyb
* @Date:2020/11/11 10:31 上午
* @Versiion:1.0
*/
@RestController
public class ExcelController {
/**
* 導出
*/
@RequestMapping("/export")
public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
//模拟需要導出的資料
List<Student> list = new ArrayList();
Student student = new Student();
student.setId(1);
student.setName("1321");
student.setSex("男");
student.setGrade("一年級");
for (int i = 0; i < 5; i++) {
list.add(student);
}
//設定并導出
// 這裡注意 有同學反應使用swagger 會導緻各種問題,請直接用浏覽器或者用postman
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
//設定檔案名
SimpleDateFormat fDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String fileName = "導出測試表" + fDate.format(new Date()) + ".xlsx";
// 這裡URLEncoder.encode可以防止中文亂碼 當然和easyexcel沒有關系
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
EasyExcel.write(response.getOutputStream(), Student.class).sheet("sheet1").doWrite(list);
}
}
導出Excel的資料
導入Excel資料
建立Student讀取類
StudentListener.java
package net.ybclass.online_ybclass.exceldemo;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName:StudentListener
* @Description:學生讀取類
* @Author:chenyb
* @Date:2020/11/20 1:49 下午
* @Versiion:1.0
*/
public class StudentListener extends AnalysisEventListener<Student> {
private List<Student> studentList = new ArrayList<Student>();
public StudentListener() {
super();
studentList.clear();
}
/**
* 每一條資料解析都會調用
*/
@Override
public void invoke(Student student, AnalysisContext analysisContext) {
//此處,可以做資料的校驗
studentList.add(student);
}
/**
* 所有資料解析完成都會調用
*/
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
studentList.forEach(System.out::println);
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
}
package net.ybclass.online_ybclass.exceldemo;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
/**
* @ClassName:Student
* @Description:Excel導出實體類
* @Author:chenyb
* @Date:2020/11/11 10:30 上午
* @Versiion:1.0
*/
@ExcelIgnoreUnannotated()
public class Student {
private Integer myId;
@ExcelProperty(value = "學号",index = 0) //0 對應導出Excel表格的第一列
private Integer id;
@ExcelProperty(value = "姓名",index = 1)
private String name;
@ExcelProperty(value = "性别",index = 2)
private String sex;
@ExcelProperty(value = "班級",index = 3)
private String grade;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", grade='" + grade + '\'' +
'}';
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
/**
* 導入學生資訊
*
* @param file
* @throws IOException
*/
@RequestMapping(value = "import")
public List<Student> importStudentInfos(MultipartFile file) throws IOException {
StudentListener studentListener = new StudentListener();
EasyExcel.read(file.getInputStream(), Student.class, studentListener).sheet().doRead();
return studentListener.getStudentList();
}
作者:陳彥斌 出處:https://www.cnblogs.com/chenyanbin/ 關注: 點我喲(^U^)ノ~YO
個性簽名:沒有學不會的技術,隻有不學習的人! 聯系方式:543210188(WeChat/QQ), 「推薦WeChat」