downloadTable(){
//1.使用element-ui提供的服务式loading方法
const options = {
spinner: "el-icon-loading",
text: "下载中...",
background: "rgba(0, 0, 0, 0.4)",
};
const loadingInstance = Loading.service(options);
//2.调用后端的接口
this.axios.get(`路径地址?键=${值}`,{responseType: "blob",}).then(res=>{
this.downloadLoading = false;
//3.使用下载方法
this.downloadExcel(data, 'xx列表.xlsx');
})
},
downloadExcel(data, fileName, type){
//1.如果没有传type 默认给type赋值
if (!type) type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8';
//2.创建一个Blob对象
let blob = new Blob([data], { type: type });
//3.如果浏览器支持msSaveOrOpenBlob方法
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName)
}
//4.如果浏览器不支持msSaveOrOpenBlob方法
else{
let alink = document.createElement('a'); //创建一个a标签
document.body.appendChild(alink);//把a标签添加到body中
alink.href = window.URL.createObjectURL(blob); //设置a标签的下载链接路径
alink.download = fileName; //下载后文件名
alink.click(); //点击下载
document.body.removeChild(alink); //下载完成移除a标签
window.URL.revokeObjectURL(alink.href); //释放掉blob对象
}
}
Vue中导出下载excel文件
最新推荐文章于 2024-04-13 06:54:22 发布