使用JavaScript将大型HTML表格导出到Excel

5
我正在使用以下代码从HTML表格生成Excel文件,在小规模数据集上运行良好,但在大型HTML表格数据集上会显示下载错误。
   //creating a temporary HTML link element (they support setting file names)
    var a = document.createElement('a');
    //getting data from our div that contains the HTML table
    var data_type = 'data:application/vnd.ms-excel';
    var table_div = document.getElementById('dataTable');
    var table_html = table_div.outerHTML.replace(/ /g, '%20');
    a.href = data_type + ', ' + table_html;
    //setting the file name
    a.download = 'Sample.xls';
    //triggering the function
    a.click();
    //just in case, prevent default behaviour
    e.preventDefault();  
2个回答

2

1
解析大型HTML表格可能会非常缓慢,因为有很多DOM元素。请考虑使用基于纯HTML5画布的数据网格,例如这个(https://github.com/openfin/fin-hypergrid)或类似的东西。
此外,请考虑保存为CSV是否比保存为xls更快。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接