vue3导出excel

170 阅读1分钟

后端返回blob格式

image.png

excel乱码加上这个就解决了

{type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}

完整代码

const exportExcelUserList = (params:any) =>
request({
  url: API.exportExcelUserList,//你的请求地址
  method: 'get',
  responseType: 'blob',
  params,//params是一个对象。get的对象传参方式
})
const exportExcel = () => {
    exportExcelUserList(queryParamas())  //接口地址
    .then((res:any) => {
      let blob = new Blob([res],{type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
      let url = window.URL.createObjectURL(blob); // 创建 url 并指向 blob
      let a = document.createElement("a");
      a.href = url;
      a.download = `会员列表.xlsx`;
      a.click();
    })
    .catch((error) => {
      reject(error.toString());
    });
};