簡體   English   中英

如何使用angularjs / javascript將HTML表導出到Excel

[英]How to export HTML table to excel using angularjs/ javascript

我已經寫了下面的代碼來下載文件。 它正在為有限的記錄工作。 現在我的記錄數超過12000。然后它掛起了瀏覽器,文件未下載

var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
var edgetype = ua.indexOf("Edge");
var blobObject;
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
    expExcelIframe.document.open("txt/html", "replace");
    expExcelIframe.document.write(html);
    expExcelIframe.document.close();
    expExcelIframe.focus();
    expExcelIframe.document.execCommand("SaveAs", true, fileName);
}
else if (edgetype > 0) {
    blobObject = new Blob([html]);
    window.navigator.msSaveOrOpenBlob(blobObject, fileName);
}
else {
    window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
}

替換encodeURIComponent

 window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html)); 

更好

var blob = new Blob([html],{type: 'data:application/vnd.ms-excel' });
var u = URL.createObjectURL(blob);
window.open(u);

這避免了轉換為base64所需的占用大量內存和計算資源的步驟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM