easyexcel版本
com.alibaba easyexcel2.2.10 org.apache.poi poi-ooxmlpoi org.apache.poi org.apache.poi poi-ooxml3.17 org.apache.poi poi-ooxml-schemas3.17 org.apache.poi poi3.17
POST方式
后台:
@RequestMapping("/excel")
public class TestController {
@PostMapping(value = "/export")
public void export(HttpServletResponse response) throws Exception {
List userList = getUserList();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + System.currentTimeMillis() + ".xlsx");
response.setCharacterEncoding("utf-8");
try{
EasyExcel.write(response.getOutputStream(), baseProductClassDto.class)
.sheet("sheet1")
.doWrite(baseProductClassList);
}catch (Exception e) {
e.printStackTrace();
}
}
private List getUserList(){
List userList = new ArrayList<>();
User user = new User();
user.setUserName("张三");
user.setSex("男");
user.setAge("10");
userList.add(user);
return userList;
}
}
前端vue:
数据导出
exportData() {
// const headers = { 'Content-Type': 'application/octet-stream' }
axios.post('/excel/export',{},
{ responseType: 'blob' }).then((res) => {
const link = document.createElement('a')
const blob = new Blob([res], { type: 'application/vnd.ms-excel'})
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
link.download = '用户列表.xlsx'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
}
导出结果:
GET方式
后台:
@RequestMapping("/excel")
public class TestController {
@GetMapping(value = "/export")
public void export(HttpServletResponse response) throws Exception {
List userList = getUserList();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + System.currentTimeMillis() + ".xlsx");
response.setCharacterEncoding("utf-8");
try{
EasyExcel.write(response.getOutputStream(), baseProductClassDto.class)
.sheet("sheet1")
.doWrite(baseProductClassList);
}catch (Exception e) {
e.printStackTrace();
}
}
private List getUserList(){
List userList = new ArrayList<>();
User user = new User();
user.setUserName("张三");
user.setSex("男");
user.setAge("10");
userList.add(user);
return userList;
}
}
前端vue:
axios({url: '/excel/export',method: 'get',responseType: 'blob'})
.then((res) => {
const link = document.createElement('a')
const blob = new Blob([res], { type: 'application/vnd.ms-excel'})
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
link.download = '用户列表.xlsx'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
导出结果:
参考资料:
vue+axios 与spring boot EasyExcel实现后台导出excel并下载_feng2036的博客-CSDN博客
springboot+vue+easyexcel导出_Desire-C的博客-CSDN博客



