exportExcel() {
let param = {
userIds: JSON.stringify(this.selectedRowKeys.map(Number))
}
console.log(param)
if(this.selectedRowKeys.length == 0) {
this.$message.error("请先选择要导出的数据")
}else {
// downloadExcel(param).then((data)=>{
// console.log(data)
// const aLink = document.createElement("a");
// let blob = new Blob([data.data], {type: "application/vnd.ms-excel"})
// aLink.href = URL.createObjectURL(blob)
// aLink.setAttribute('download', '员工管理表' + '.xlsx') // 设置下载文件名称
// aLink.click()
// // this.$refs.loadElement.appendChild(aLink)
// })
this.$axios.post('/angeek/downloadExcel', qs.stringify(param), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded', //请求的数据类型为form data格式
},
'responseType': 'blob' //设置响应的数据类型为一个包含二进制数据的 Blob 对象,必须设置!!!
}).then(function (response) {
const blob = new Blob([response.data]);
const fileName = '账号管理.xls';
const linkNode = document.createElement('a');
linkNode.download = fileName; //a标签的download属性规定下载文件的名称
linkNode.style.display = 'none';
linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
document.body.appendChild(linkNode);
linkNode.click(); //模拟在按钮上的一次鼠标单击
URL.revokeObjectURL(linkNode.href); // 释放URL 对象
document.body.removeChild(linkNode);
// 成功之后清空选择的数据
this.selectedRowKeys = []
}).catch(function (error) {
console.log(error);
});
}
},
以上是js代码
HTML代码
<el-button @click="exportExcel">默认按钮</el-button>