前端vue项目使用fetch导出Excel表格

最近做的需求中有个导出excel,所以就发一下吧,代码可直接copy,话不多说上代码!

1. 封装fetch

首先创建一个fetchdata.js文件,简单封装一下fetch。

export function PostDataBlob(query) {
  return fetch({
    method: 'post',
    responseType: 'tch',
    data: query,
  });
}

2. 导出Excel

我的项目是vue,首先在需要下载的页面引入,然后复制 downloadItem 函数代码即可。

	import { PostData, PostDataBlob } from "@/api/fetchdata";

    // 下载
    async downloadItem(row) {
      this.loading = true;
      let params = {
        microservices: "lmall/admins/quota/vision/export",
        id: row.id,
        device_source: "web",
      }
      const data = await PostDataBlob(params);
      const link = document.createElement('a');
      link.style.display = 'none';
      link.href = window.URL.createObjectURL(
        new Blob([data.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
      );
      link.setAttribute('download', `${row.name}${this.format(new Date(), 'yyyyMMdd')}.xlsx`);
      document.body.appendChild(link);
      link.click();
      this.loading = false;
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值