简体   繁体   English

如何使用angularjs / javascript将HTML表导出到Excel

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

I have written below code to download file. 我已经写了下面的代码来下载文件。 It is working for the limited record. 它正在为有限的记录工作。 now my record count is more that 12000. Then it is hanging the browser and file not downloaded 现在我的记录数超过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));
}

Replace encodeURIComponent : 替换encodeURIComponent

SLOW

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

Better 更好

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

This avoids the memory and computation intensive step of converting to base64. 这避免了转换为base64所需的占用大量内存和计算资源的步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM