SheetJS将table转为Excel

看看我

table_to_booktable_to_sheet工具函数获取DOM的TABLE元素,并且通过子节点进行迭代。

var workbook = XLSX.utils.table_to_book(document.getElementById('tableau'));
/* DO SOMETHING WITH workbook HERE */

一个网页里面的多张表可以被转换成单个的工作表。

/* create new workbook */
var workbook = XLSX.utils.book_new();

/* convert table 'table1' to worksheet named "Sheet1" */
var ws1 = XLSX.utils.table_to_sheet(document.getElementById('table1'));
XLSX.utils.book_append_sheet(workbook, ws1, "Sheet1");

/* convert table 'table2' to worksheet named "Sheet2" */
var ws2 = XLSX.utils.table_to_sheet(document.getElementById('table2'));
XLSX.utils.book_append_sheet(workbook, ws2, "Sheet2");

/* workbook now has 2 worksheets */

另一种选择,HTML代码也可以被提取和解析。

var htmlstr = document.getElementById('tableau').outerHTML;
var workbook = XLSX.read(htmlstr, {type:'string'});
posted @ 2021-07-21 16:40  vvull  阅读(377)  评论(0编辑  收藏  举报