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

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

作者: Yl奋斗的小白 | 来源:发表于2020-01-01 13:01 被阅读0次

    作为学习笔记,我这里将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)

          })

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/ccdbuctx.html