js实现导出/下载excel文件

js实现导出/下载excel文件

javascript 复制代码
// response 为导出接口返回数据,如上图
const exportExcel = (response, fileName:string) =>{
	  const blob = new Blob([response.data], {
		 type: response.headers['content-type'] //使用获取的excel格式
	  });
	  const downloadElement = document.createElement('a')
	  const url = window.URL.createObjectURL(blob)
	  downloadElement.href = url
	  let name = fileName || 'EXCEL导出'
	  downloadElement.download = `${name}.xlsx`; //下载后文件名
	  document.body.appendChild(downloadElement);
	  downloadElement.click(); //点击下载
	  document.body.removeChild(downloadElement); //下载完成移除元素
	  window.URL.revokeObjectURL(url); //释放掉blob对象
})