axios({
method: 'get',
headers: { Authorization: `bearer ${window.store.state.token}` },
url: `${END_POINT}${obj.url}`,
responseType: 'blob',
}).then((res) => {
const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' });
const downloadElement = document.createElement('a');
const href = window.URL.createObjectURL(blob); // 创建下载的链接
downloadElement.href = href;
downloadElement.download = `${obj.label}.xls`; // 下载后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); // 点击下载
document.body.removeChild(downloadElement); // 下载完成移除元素
window.URL.revokeObjectURL(href); // 释放掉blob对象
});
第二种方案
clickExport() {
var weekData = {
startTime: this.startTime,
endTime: this.endTime,
companyId: this.weekCompanyId ? Number(this.weekCompanyId) : null,
departmentId: this.weekDepartmentId
}
// 方法
weeklyNumberExport(weekData).then(res => {
this.exportData(res, '每周人数表')
})
}
// 在调用统一方法
exportData(res, title) {
const link = document.createElement('a')
const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
link.setAttribute('download', title + '.xlsx')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
},
- 特别注意 api中需要
export function rosterExport(params) {
return request({
url: '/roster/listContractMessageExport',
method: 'get',
responseType: 'blob', /// 要单独添加
params
})
}