用法
// 导出
exportExcel() {
const data = {
methods: "post",
url: "/outExcel/outBus",
data: {
name: this.form.name,
status: this.form.status,
orderNo: this.form.orderNo,
pageNum: this.pageNum,
pageSize: this.pageSize,
},
fileName:'企业管理',
};
exportMethodExcel(data)
},
// 导出Excel公用方法
export function exportMethodExcel(data) {
loading = Loading.service({
lock: true,
text: '正在为您导出中,请稍后...',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
});
axios({
method: data.methods,
url: window.webConfig.busApi + data.url,
data: data.data,
params: data.params,
responseType: 'blob',
headers: {
['token']: getLocalStorage("token"),
["resId"]: router ?.app ?._route ?.meta ?.menuId
}
}).then((res) => {
console.log('导出封装方法', res)
let fileReader = new FileReader();
fileReader.readAsText(res.data, 'utf-8')
fileReader.onload = (e) => {
console.log('e是什', e)
try {
let jsonData = JSON.parse(e.target.result);
console.log('报错信息', jsonData)
if (jsonData.statusCode !== '200') {
Notification({
title: "文件导出错误通知",
message: jsonData.statusMsg,
type: 'error',
duration: 5 * 1000
})
loading.close();
}
} catch (err) {
const link = document.createElement('a')
let blob = new Blob([res.data], {
type: 'application/vnd.ms-excel;charset=utf-8'
})
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
link.download = data.fileName + '.xlsx' //下载后文件名
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
loading.close();
Notification({
title: "文件导出成功通知",
message: "文件导出成功,请您留意您浏览器下载",
type: 'success',
duration: 5 * 1000
})
}
}
}).catch(error => {
})
}
image.png
image.png
网友评论