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

23 lines
818 B
JavaScript

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];
/* happy-dom <= 14 add `rows` and `cells` properties */
tbl.rows = Array.from(tbl.getElementsByTagName("tr"));
if(Array.isArray(tbl.rows)) 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");