天天看點

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("導出失敗!"))