docs.sheetjs.com/docz/static/dom/SheetJSHappyDOM.js

23 lines
818 B
JavaScript
Raw Normal View History

2023-09-11 04:44:15 +00:00
const XLSX = require("xlsx");
const { readFileSync } = require("fs");
const { Window } = require("happy-dom");
/* obtain HTML string. This example reads from SheetJSTable.html */
const html_str = readFileSync("SheetJSTable.html", "utf8");
/* get table element */
const window = new Window({
url: "https://localhost:8080",
width: 1024,
height: 768
});
window.document.body.innerHTML = html_str;
const tbl = window.document.body.getElementsByTagName("table")[0];
2024-10-31 04:25:02 +00:00
/* happy-dom <= 14 add `rows` and `cells` properties */
2023-09-11 04:44:15 +00:00
tbl.rows = Array.from(tbl.getElementsByTagName("tr"));
2024-10-31 04:25:02 +00:00
if(Array.isArray(tbl.rows)) tbl.rows.forEach(row => row.cells = Array.from(row.getElementsByTagName("td")))
2023-09-11 04:44:15 +00:00
/* generate workbook */
const workbook = XLSX.utils.table_to_book(tbl);
XLSX.writeFile(workbook, "SheetJSHappyDOM.xlsx");