使用javascript将大型html表导出为ex​​cel

时间:2016-12-26 04:30:34

标签: javascript html excel file-conversion

我使用以下代码从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 个答案:

答案 0 :(得分:1)

我找到了一种使用FileSaver.js解决此问题的方法   1https://github.com/eligrey/FileSaver.js/请检查以下内容

HTML enter image description here

的Javascript

enter image description here

对于HTML表格的大数据集,这对我来说很好。

答案 1 :(得分:0)

由于有很多DOM元素,解析大型HTML表格可能会非常慢。请考虑使用纯HTML5基于画布的数据网格(如https://github.com/openfin/fin-hypergrid)或其他内容。

另外考虑保存为CSV会比保存为xls更快。