首页 > 解决方案 > JavaScript 中的 Excel 导出无法使用 Blob

问题描述

我有一些javascript问题。我正在努力下载 xlsx 格式的文件。在后端工作一切正常 - 使用例如 swagger 下载文件。

我的代码:

private async downloadDocumentTemplate(report: string) {
            this.fileLoading = true;
            try {
                const result = await this.$store.dispatch("report/downloadDocument", report);

                const blob = new Blob([result], {type: "application/octet-stream"});

                var reader = new FileReader();
                reader.addEventListener("loadend", function() {

                });
                reader.readAsArrayBuffer(blob);


                console.log("blob contnet : " + blob.type);

                FileSaver.saveAs(blob, 'report.xlsx');
            } catch (ex) {
                this.$store.commit("app/showErrorPopup", ex);
            }
            this.fileLoading = false;
        }

从网上下载文件后,文件自动打开,我遇到如下错误:

“我们发现某些内容 .xlsx 存在问题。您希望我们尽可能多地尝试恢复吗?” e

标签: javascriptvue.js

解决方案


我有一些类似的问题。我的问题是我如何解析 Excel 数据。脚本输入了错误的格式信息。

如果可以将文件下载到计算机上,则解析 excel 数据的代码中一定有错误。


推荐阅读