前端导出excel

85 阅读1分钟

重点:get请求要加 reponseType:'blob'

企业微信截图_1719475833159.png 不然结果就是可以导出,但是文件打不开

企业微信截图_17194581421981.png

const exportFn = () => {
	apiExportExcel(params).then(res => {
         // res 为后端返回的数据流
		let blob = new Blob([res], { type: 'application/vnd.ms-excel' })
		// 创建 href 超链接,点击进行下载
		window.URL = window.URL || window.webkitURL
		let href = URL.createObjectURL(blob)
		let downA = document.createElement('a')
		downA.href = href
		downA.download = '隐患台账.xls'
		downA.click()
		// 销毁超连接
		window.URL.revokeObjectURL(href)
	})
}