项目中导出数据excel方法有多种,这里讲一下通过后端返回excel文件,导出excel
import request from '@/utils/request'
export function getDownLoad(params) {
return request({
url: `/api/Report/getMaterialTracking`,
method: 'get',
params,
responseType: 'blob',
headers: {
'Content-Type': 'application/x-download'
}
})
}
//下载数据
handleDownload() {
let query={
...this.searchForm,
isExport:true,
};
getDownLoad(query).then((res) => {
console.log(res);
const link = document.createElement("a");
link.style.display = "none";
link.href = URL.createObjectURL(res);
link.setAttribute("download", "表格名字.xlsx");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
},
responseType: 'blob', // 表明返回服务器返回的数据类型,不加会导致乱码