exportRequest.ts 1021 Bytes
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();
    });
};