使用 javascript 将大 html table 导出到 excel
Export large html table to excel using javascript
我正在使用以下代码从 html table 生成 excel 文件,它适用于小型数据集,当涉及到大型 html table 数据集显示下载错误。
//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();
解析大型 HTML 表可能会非常慢,因为有很多 DOM 元素。请考虑使用纯 HTML5 canvas 的数据网格,例如这个 (https://github.com/openfin/fin-hypergrid) 或类似的东西。
还要考虑另存为 CSV 是否比另存为 xls 更快。
我找到了一种解决此问题的方法 FileSaver.js
1: https://github.com/eligrey/FileSaver.js/ 请检查以下内容
HTML
Javascript
对于 HTML table 的大数据集,这对我来说工作正常。
我正在使用以下代码从 html table 生成 excel 文件,它适用于小型数据集,当涉及到大型 html table 数据集显示下载错误。
//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();
解析大型 HTML 表可能会非常慢,因为有很多 DOM 元素。请考虑使用纯 HTML5 canvas 的数据网格,例如这个 (https://github.com/openfin/fin-hypergrid) 或类似的东西。
还要考虑另存为 CSV 是否比另存为 xls 更快。
我找到了一种解决此问题的方法 FileSaver.js 1: https://github.com/eligrey/FileSaver.js/ 请检查以下内容
HTML
Javascript
对于 HTML table 的大数据集,这对我来说工作正常。