vue中导出后台返回的二进制数据为excel文件到本地

2020-01-01  本文已影响0人  Yl奋斗的小白

作为学习笔记,我这里将getExcel方法封装起来然后全局调用的,不封装的写在后面

引入getExcel方法  这样就OK啦

不封装的

axios({

            method: 'GET',

            url: '/api',

            params: params,

            responseType: 'blob'

     })then(res => {

        const link = document.createElement('a')

        let blob = new Blob([res],{type: 'application/vnd.ms-excel'});

        link.style.display = 'none'

        link.href = URL.createObjectURL(blob);

        let num = ''

        for(let i=0;i < 10;i++){

            num += Math.ceil(Math.random() * 10)

        }

        link.setAttribute('download', '公共代码_' + num + '.xlsx')

        document.body.appendChild(link)

        link.click()

        document.body.removeChild(link)

      })

上一篇下一篇

猜你喜欢

热点阅读