有俩种方法可以用
第一种方法:
解析:直接解析并且下载后端的乱码
this.download('后端给的导出excel的方法', {
...this.queryParams
}, `job_${new Date().getTime()}.xlsx`)
第二种方法:
封装一个api,里面写你的方法,在请求的方法里加入:
responseType: 'blob',
例如:
//导出表格数据 export function exportTable() { return request({ url: '/portal/course/export', method: 'post', responseType: 'blob', }) }
在 .vue 文件引入了这个方法之后,方法里写上这些代码。
例如:
exportTable().then(res=>{ const fileName = '表格数据.xls'; //表格名字 if ('download' in document.createElement('a')) { // 非IE下载 const blob = new Blob([res], {type: 'application/ms-excel'}); // 解析后端返回的乱码 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); } }).catch(()=>{ })