当前位置:   article > 正文

excel下载两种方式(axios和window.open)_window.open 下载excel

window.open 下载excel

excel下载两种方式

1

	第一种 调用
		window.open
				methods:{
					    getDown() {
				      window.open(baseUrl+ "/report/data_excel?curDay=" +this.date + "&token=" +localStorage.getItem("token") );
				    },
				}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2

第二种 使用axios 
	动态创建a标签
	使用a标签的href属性
	a.click()
	a.remove()



js代码
       excelMonse() {
           const a = document.createElement('a')
           axios({
               responseType: 'blob',  //必须的
               method: "get",
               url: "/member/account_list"
           }).then(res => {
               // // 切割出文件名
               // const fileNameEncode = res.headers['content-disposition'].split('filename=')[1] 
               // 解码
               // const fileName = decodeURIComponent(fileNameEncode)
               console.log(res.headers);
               const blob = new Blob([res.data], {
                   type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; application/octet-stream'
               })
               const fileUrl = window.URL.createObjectURL(blob)
               a.href = fileUrl
               console.log('url', fileUrl)
               a.setAttribute('download', 'fileName') //固定的文件名称
               //a.setAttribute('download', fileName)//动态文件名称
               a.style.display = 'none'
               a.click()
               a.remove()
           })
       },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/770433
推荐阅读
相关标签
  

闽ICP备14008679号