vue实现导入导出

导出

<el-button
  type="primary"
  size="small"
  :disabled="!tableData.length
  @click="Export"
>
导出
</el-button>
async Export() {
      //调接口
      const res = await mainTaskStaticExportApi({
        startYear: this.search.startYear,
        endYear: this.search.endYear,
        subjectCodeMap: {},
      });

      if (res.status === 200 || res.status === '200') {
       // 对接口返回的数据进行处理
        const blob = new Blob([res.data]);
       // 文件名字
        const fileName =
          res.headers['content-disposition'].split('filename=')[1];
        if ('download' in document.createElement('a')) {
          // 支持a标签download的浏览器
          const link = document.createElement('a'); // 创建a标签
          link.download = decodeURI(fileName); // a标签添加属性
          link.style.display = 'none';
          link.href = URL.createObjectURL(blob);
          document.body.appendChild(link);
          link.click(); // 执行下载
          URL.revokeObjectURL(link.href); // 释放url
          document.body.removeChild(link); // 释放标签
        } else {
          // 其他浏览器
          navigator.msSaveBlob(blob, fileName);
        }
      }
    },

猜你喜欢

转载自blog.csdn.net/m0_56274171/article/details/124433247