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

Dim*_*thu 5 html javascript 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();  
Run Code Online (Sandbox Code Playgroud)

Dim*_*thu 2

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

超文本标记语言 在此输入图像描述

JavaScript

在此输入图像描述

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