vue 下载excel文件流

1.设置请求头 responseType: 'blob'

后端接口

//导出
export function downloadCustomMechanismInfo(data){
    return request({
        url: '下载的接口地址',
        method: 'post',
        data,
        responseType: 'blob' 
    })
}

2.导出excel文件

import { Loading } from 'element-ui'
//方法一
export default {
    methods: {
        exportAllList(){
      const loadingInstance = Loading.service({
        fullscreen: true,
        background: '#fff0'
      })
      downloadCustomMechanismInfo({}).then(res=>{
        loadingInstance.close()
        const content = res
        const blob = new Blob([content])//处理返回
        const fileName = '自定义主机构信息.xls'
        if ('download' in document.createElement('a')) {
          // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else {
          // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    }
}




//方法二
downloadBtn() {
      if (this.selectList.length === 0) {
        this.$notify({
          title: '提示',
          message: '请先选中需要下载的数据',
          type: 'warning'
        })
        return false
      }
      const url =
        process.env.VUE_APP_BASE_API +
        '/bzcounterparty/exportExcel?ids=' +this.downloadData //下载数据需要传的数据
      window.open(url)
    },


//方法三
<script>
export default {
  methods: {
    //导出
    exportProduct(){
      const param = {
        bondShortName:this.form.bondShortName,
        compname:this.form.compname,
        fundName:this.form.fundName,
        startDate:this.form.startDate,
        endDate:this.form.endDate
      };
        //导出接口
      exportProductConcentration(param).then( res => {
        settleDocuments(res);
      }).catch(err =>{
        console.log(err);
      })
    },
  }
};
// 导出方法
function settleDocuments(content) {
  console.log(content);
  // 处理返回的文件流
  const blob = new Blob([content])
  const link = document.createElement('a')
  link.href = window.URL.createObjectURL(blob)
  link.download = '信息.xls'
  link.click()
}
</script>

3.请求拦截中添加responseType类型

if (config.url.indexOf('exportPledgeConcentration') != -1 || config.url.indexOf('exportProductConcentration') != -1) {
            config['responseType'] = 'blob'
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值