exportRequest.ts
1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import axios from 'axios';
export const excelExport = async (
url: any = '',
data: any = {},
exportLoadingDestory: any,
) => {
axios({
url: url,
method: 'post',
responseType: 'blob',
headers: { Authorization: localStorage.getItem('token') },
data,
})
.then((response) => {
// 我这里在拦截器里直接返回的response.data
const body = response.data;
let fileUrl = window.URL.createObjectURL(
new Blob([body], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
}),
);
let a = document.createElement('a');
a.style.display = 'none';
a.href = fileUrl;
a.download = '开票记录.xlsx';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(a.href);
document.body.removeChild(a);
})
.catch((error) => {
// 处理错误
console.error('导出错误', error);
})
.finally(() => {
exportLoadingDestory();
});
};