调用后台接口,将表格导出为excel文件
前言
在vue项目中通常遇到将table数据导出为excel文件的情况,由于记性有点差, 还是记下来我常用的方法吧导出方法
1.引入axios库
提示:axios安装就不详述啦,网上很多资料
代码如下(示例):
import axios from 'axios'
2.请求接口
代码如下(示例):
// 调用后台接口需要传的参数(此处为示例,具体参数根据后端提供的接口为准)
let data = {
name: 'zhangsan',
time: 1645437800000
}
//axios 请求
axios({
method: 'post',
url: process.env.VUE_APP_BASE_API + 'bms/exchange/export',
// process.env.VUE_APP_BASE_API 为接口服务路径
timeout: 60 * 10 * 1000,
data: data,
responseType: 'blob',
headers: {
'accessToken': window.sessionStorage.getItem('token')
},
}).then((res) => {
const url = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", `record_${new Date().getTime()}.xlsx`);
document.body.appendChild(link);
link.click();
link.remove();
}).finally(() => {
})