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

23 lines
774 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];
/* add `rows` and `cells` properties */
tbl.rows = Array.from(tbl.getElementsByTagName("tr"));
tbl.rows.forEach(row => row.cells = Array.from(row.getElementsByTagName("td")))
/* generate workbook */
const workbook = XLSX.utils.table_to_book(tbl);
XLSX.writeFile(workbook, "SheetJSHappyDOM.xlsx");