美文网首页
angular2下载excel兼容ie

angular2下载excel兼容ie

作者: Cheng丶 | 来源:发表于2018-05-23 16:54 被阅读0次

postUrl为请求地址,postBody为请求体,operate为导出文件名

 exportTarget(postUrl, postBody, operate) {

        let today = moment().format('YYYY-MM-DD');

        this.http.post(postUrl, postBody, { responseType: 3 }).map(res => res.json()).subscribe(res => {

            // let blob = new Blob([res], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });

            // let blob = new Blob([res], { type: "application/vnd.ms-excel" });

            let blob = new Blob([res], { type: "application/msexcel;charset=UTF-8" });

            if (window.navigator.msSaveOrOpenBlob) {

                navigator.msSaveBlob(blob, operate + today + '.xls');

            } else {

                let resUrl = URL.createObjectURL(blob);

                let a = document.createElement('a');

                document.body.appendChild(a);

                a.setAttribute('style', 'display:none');

                a.setAttribute('href', resUrl);

                a.setAttribute('download', operate + today + '.xls');

                a.click();

                // document.body.removeChild(a);

                // 释放url

                URL.revokeObjectURL(resUrl);

            }

        })

    }

相关文章

网友评论

      本文标题:angular2下载excel兼容ie

      本文链接:https://www.haomeiwen.com/subject/dlhddxtx.html