項目使用的axios這里一axios為例
首先在發(fā)起請求前的設(shè)置:
const ajax = axios.create({responseType:"arraybuffer"})
設(shè)置responseType類型,如果不設(shè)置下載的excel會亂碼
然后處理返回的數(shù)據(jù)
let res = //你要發(fā)起請求的接口
let blob = new Blod([res.data],{type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})
const BlobUrl = window.URL.createObjectURL(blob)
let a = document.createElement('a')
//a.download = fileName // 如若需要自定義文件名則加上這句
a.href = BlobUrl
a.click()
URL.revokeObjectURL(blobUrl)
document.body.removeChild(a)
//最后銷毀URL和移除a標簽