const exportToWord = (response, filename) => {
const elink = document.createElement('a');
elink.style.display = 'none';
const blob = new Blob([response], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
});
const blobUrl = URL.createObjectURL(blob);
elink.href = blobUrl;
elink.download = filename; // 设置下载后的文件名
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
URL.revokeObjectURL(blobUrl);
};
const handleDownload = async () => {
try {
const res = await downloadElementDaily({
archive_date: selectedDate.format('YYYY-MM-DD'),
});
exportToWord(res, '资产异常列表.xlsx');
} catch (error) {
console.error('Error downloading data:', error);
}
};
定义接口时,一定要加上responseType: ‘blob’
export async function downloadElementDaily(params) {
return request('/server/v1/cost/element/daily/download', {
method: 'GET',
params: { ...params },
responseType: 'blob',
});
}