欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue下载excel的实现代码后台用post方法

程序员文章站 2023-12-13 15:19:22
后台方法的参数必须是@requestbody修饰的。       前台关键代码:   ...

后台方法的参数必须是@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下载excel的实现代码后台用post方法,希望对大家有所帮助

上一篇:

下一篇: