vue 中实现文件excel 下载

exportExcel() {
      let param = {
        userIds: JSON.stringify(this.selectedRowKeys.map(Number))
      }
      console.log(param)
      if(this.selectedRowKeys.length == 0) {
        this.$message.error("请先选择要导出的数据")
      }else {
        // downloadExcel(param).then((data)=>{
        //   console.log(data)
        //   const aLink = document.createElement("a");
        //   let blob = new Blob([data.data], {type: "application/vnd.ms-excel"})
        //   aLink.href = URL.createObjectURL(blob)
        //   aLink.setAttribute('download', '员工管理表' + '.xlsx') // 设置下载文件名称
        //   aLink.click()
        //   // this.$refs.loadElement.appendChild(aLink)
        // })
        this.$axios.post('/angeek/downloadExcel', qs.stringify(param), {
          headers: {
              'Content-Type': 'application/x-www-form-urlencoded', //请求的数据类型为form data格式
            },
          'responseType': 'blob'  //设置响应的数据类型为一个包含二进制数据的 Blob 对象,必须设置!!!
        }).then(function (response) {
          const blob = new Blob([response.data]); 
          const fileName = '账号管理.xls';
          const linkNode = document.createElement('a');
          linkNode.download = fileName; //a标签的download属性规定下载文件的名称
          linkNode.style.display = 'none';
          linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
          document.body.appendChild(linkNode);
          linkNode.click();  //模拟在按钮上的一次鼠标单击
          URL.revokeObjectURL(linkNode.href); // 释放URL 对象
          document.body.removeChild(linkNode);
          // 成功之后清空选择的数据
          this.selectedRowKeys = []
        }).catch(function (error) {
            console.log(error);
        });
      }
    },

以上是js代码

HTML代码

<el-button @click="exportExcel">默认按钮</el-button>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值