vue导出excel文档(后端返回数据流格式)

1.新增一个js文件(downloadFile.js)

import { _axios } from "../assets/request";
import  Vue  from "vue";
//url为导出的地址
//fileName为导出的文件名称
//methods是使用post或者get方法请求
function  downloadFile(url,fileName,methods) {
    if(methods=="post"){
        _axios.post(url, { responseType: 'blob' }).then(res => {
            const blob = new Blob([res.data], { type: 'application/vnd.ms-excel; charset=utf-8' })
            const link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.download = fileName //下载的文件名称
            link.click()
            URL.revokeObjectURL(link.href)
            Vue.prototype.$message({
              message: "导出成功~",
              type: "success",
            });
          }).catch(err => {
            console.log(err,"导出失败的错误信息")
            Vue.prototype.$message({
              message: "导出失败~",
              type: "error",
            });
          })
    }else{
        _axios.get(url, { responseType: 'blob' }).then(res => {
            const blob = new Blob([res.data], { type: 'application/vnd.ms-excel; charset=utf-8' })
            const link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.download = fileName //下载的文件名称
            link.click()
            URL.revokeObjectURL(link.href)
            Vue.prototype.$message({
              message: "导出成功~",
              type: "success",
            });
          }).catch(err => {
            console.log(err,"导出失败的信息")
            Vue.prototype.$message({
              message: "导出失败~",
              type: "error",
            });
          })
    }
  }

export  {downloadFile}

 2.在页面引入封装的js文件

import { downloadFile } from "../../assets/downloadFile"

3.使用

exportEXL() {
  downloadFile(`地址`, "需要导出的文件名", "接口请求方法(get/post)")
},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值