使用js导出excel,上万条数据,我该怎么办?



我使用了以下方法,但它不适合

export = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta charset="UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) ;}
, format = function(s, c) { return s.replace(/{(w+)}/g, function(m, p) { return c[p]; }); };
return function(table, name,filename) {
if (!table.nodeType) table = document.getElementById(table);
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
// window.location.href = uri + base64(format(template, ctx));
document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = "finame";
document.getElementById("dlink").click();
};
})();

您可能希望在服务器上创建文件,并通过 AJAX 调用或 websocket 提供该文件。

主要问题是客户端必须解析所有这些数据,并且根据客户端的不同,它可能非常慢/繁重。

还要记住,如果您的表是分页的,则需要手动选择每个页面,以将表解析为 1 个大结果集。

根据Excel的规范,从Excel 2007及更高版本开始,每个工作簿应有1,048,576行。

相关内容

最新更新