设为首页 加入收藏

TOP

(办公)vue下载excel,后台用post方法
2019-09-17 18:26:38 】 浏览:14
Tags:办公 vue 下载 excel 后台 post 方法

  后台方法的参数必须是@RequestBody修饰的。

      前台关键代码:

     

axios ( {
          method : 'post',
          url : api.exportPlayTime , // 请求地址
          data : {
            choose : type,
            begindate : startDate,
            enddate : endDate
          },
          responseType : 'arraybuffer',
          observe: 'response',
        } )
          .then ( ( res ) => {

            const fileName = ""+filename+".xlsx"
            let blob = new Blob([res.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
            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 )
            }

          })

 

download(data) {
        if (!data) {
          return
        }
        let url = window.URL.createObjectURL(new Blob([data]))
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', 'excel.xlsx')

        document.body.appendChild(link)
        link.click()
      },

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇记录vue创建项目过程 下一篇最新的JavaScript知识总结,欢迎..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目