使用axios下载Excel文件

276 阅读1分钟
const handleExport = async () => {
    const {
      department = '',
      name = '',
      score = '',
      grade = ''
    } = params.queryEntity || {};
    const param = { department, name, grade };
    const res = await exportExcel(param);
    if (res?.data) {
      try {
        const blob = new Blob([res.data], {
          type: 'application/vnd.ms-excel;charset=utf-8'
        });
        const objectUrl = URL.createObjectURL(blob);
        const link = document.createElement('a');
        const fileName = '报告';
        link.href = objectUrl;
        link.setAttribute('download', fileName);
        document.body.appendChild(link);
        link.click();
      } catch (error) {
        console.log(error);
      }
    }
  };