下载Blob xlsx文件

问题描述 投票:0回答:1

我是一个非常新的角度我需要创建blob xlsx文件。我尝试了很多不同的方法,比如将内容类型更改为不同的格式等,但没有任何帮助。当我尝试通过Excel加载xlsx文件时出错。如果我直接通过URL下载它可以正常工作。我不知道出了什么问题。

t.prototype.downloadXlsx = function () {
            var t = this, e = {
                date_from: this.helpers.formatDate(this.filter.date_from),
                date_to: this.helpers.formatDate(this.filter.date_to),
                download: "fees"
            };
            this.http.get("team-accounts/" + this.route.snapshot.params.id, e).subscribe(function (n) {
                var i = {type: 'application/octet-stream'},
                    r = t.account.team.name + "_summary_" + e.date_from + "_" + e.date_to + ".xlsx";
                t.helpers.downloadXlsxFile(n._body, i, r);
            })

t.prototype.downloadXlsxFile = function (t, e, n, i) {
            var r = new Blob(t], e);
            if (navigator.msSaveBlob) navigator.msSaveBlob(r, n); else {
                var a = document.createElement("a");
                if (void 0 !== a.download) {
                    var o = i || document.body, s = URL.createObjectURL(r);
                    a.setAttribute("href", s), a.setAttribute("download", n), a.style.visibility = "hidden", o.appendChild(a), a.click(), o.removeChild(a)
                }
            }
excel angular blob xlsx
1个回答
1
投票

你可以这样试试,


t.prototype.downloadXlsx = function () {
   var t = this, e = {
        date_from: this.helpers.formatDate(this.filter.date_from),
        date_to: this.helpers.formatDate(this.filter.date_to),
        download: "fees"
   };
   this.http.get("team-accounts/" + this.route.snapshot.params.id, e).subscribe( n => {

        var url = t.account.team.name + "_summary_" + e.date_from + "_" + e.date_to + ".xlsx";

       // file download section
       const a = document.createElement('a');
       document.body.appendChild(a);
       const blob = new Blob([atob(n['_body'])], { type: 'octet/stream' });
      // if octet/stream is not working then try this one application/ms-excel
       const url = window.URL.createObjectURL(blob);
       a.href = url;
       a.download = "filename.xlsx"; // you need to write the extension of file here
       a.click();
       window.URL.revokeObjectURL(url);
    })

我希望它可以帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.