1.代码
- axios.get(`/ExcelExamLogByExamPlan?id=123`,{responseType: 'blob'}).then(res => {
- const data = res.data
- const url = window.URL.createObjectURL(new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}))
- const link = document.createElement('a')
- link.style.display = 'none'
- link.href = url
- link.setAttribute('download', 'excel.xls')
- document.body.appendChild(link)
- link.click()
- document.body.removeChild(link)
- })
2.后记
文件下载后windows自带的excel打开始终提示格式类型错误,但是用wps可以打开,后来发现setAttribute部分设置把xlsx
改为xls
即可