欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Vue.js + SpringBoot 后台导出Excel表格

程序员文章站 2022-06-06 20:02:30
...

1、前端Vue.js

var data = this.$store.getters.excel.data;
let url = window.URL.createObjectURL(new Blob([data]))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.download = 'XX.xlsx';
document.body.appendChild(link)
link.click()
window.URL.revokeObjectURL(link.href);


// 不能用拦截器
export function exportExcel(){
  return axios({
    url: process.env.BASE_API+'/api/export',
    method: 'post',
    headers: {"Authorization":+ getToken()},
    responseType: 'blob',
    data: {}
  })
}

2、后端

 

Workbook book = ....


HttpServletResponse response

response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-disposition", "attachment;fileName=XX.xlsx");
OutputStream out = response.getOutputStream();
out.flush();
book.write(out);