1.代码

  1. axios.get(`/ExcelExamLogByExamPlan?id=123`,{responseType: 'blob'}).then(res => {
  2. const data = res.data
  3. const url = window.URL.createObjectURL(new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}))
  4. const link = document.createElement('a')
  5. link.style.display = 'none'
  6. link.href = url
  7. link.setAttribute('download', 'excel.xls')
  8. document.body.appendChild(link)
  9. link.click()
  10. document.body.removeChild(link)
  11. })

2.后记

文件下载后windows自带的excel打开始终提示格式类型错误,但是用wps可以打开,后来发现setAttribute部分设置把xlsx改为xls即可

作者 铁血 汉子 2021年7月2日
2025/05/18/03:00:17pm 2021/7/2/5:50:15
0 1025