这是最新的业务,要求是把el-table中的选中的条数转换成excel并下载下来,废话不多说,直接干代码
let data = this.$refs.multipleTable.selection这个是ref为multipleTable的表格
先获取选中的行,然后当没有选中的时候不要导出,防止重复调用后端接口.
if (this.$refs.multipleTable.selection.length == 0) {
this.$message({
type: 'error',
message: '请选择后导出'
});
return false
}
后面进行数据处理,不方便展示了,下面是发送axios,不要封装,不要封装,不要封装.不然一定会乱码。
axios({ // 用axios发送post请求
method: 'post',
url: '', // 请求地址
data: this.datas,
responseType: 'blob', // 表明返回服务器返回的数据类型
headers: {
'Content-Type': 'application/json'
}
}).then(res => { // 处理返回的文件流
//new Blob([res])中不加data就会返回下图中[objece objece]内容(少取一层)
const blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
const fileName = '字典数据.xlsx';//下载文件名称
const elink = document.createElement('a');
elink.download = fileName;
elink.style.display = 'none';
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
})