繁体   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