前端输出excel时,excel无法打开 请求时加入responseType: "blob"

2,332 阅读1分钟

前端输出excel时,excel无法打开 1、请求时加入responseType: "blob" 2、前后端excel格式要对应。 xlsx xls

function deriveEXCEL(data, name) {
    if (!data) {
        return
    }
    const url = window.URL.createObjectURL(new Blob([data]))
    const link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.setAttribute('download', 'excel.xlsx')

    document.body.appendChild(link)
    link.download = `${name}.xlsx`
    link.click()
    document.body.removeChild(link) // 下载完成移除元素
    window.URL.revokeObjectURL(url) // 释放掉blob对象
}