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即可

作者 铁血 汉子 2021年7月2日
2024/04/25/10:18:53pm 2021/7/2/5:50:15
0 801