天天看点

react和fetch导出Excel表格

常见格式:

文件后缀 type
.doc application/msword
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.xls application/vnd.ms-excel
.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.ppt application/vnd.ms-powerpoint
.pptx
URL.createObjectURL()
fetch(config.url + "report/download", {
                    headers: {
                        "token": token
                    }
                }).then(res => res.blob().then(blob => {
                    var a = document.createElement('a');
                    var url = window.URL.createObjectURL(new Blob([blob], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }));   // 获取 blob 本地文件连接 (blob 为纯二进制对象,不能够直接保存到磁盘上)
                    a.href = url;
                    a.download = "报告";  //定义导出的文件名
                    a.click();
                    window.URL.revokeObjectURL(url);
                })).catch(err => message.error("导出失败!"))