docs.sheetjs.com/docz/static/sqlite/SheetJSQLiteNode.mjs

32 lines
871 B
JavaScript
Raw Permalink Normal View History

2023-05-29 02:52:54 +00:00
/* Load SQLite3 connector library */
import Database from "better-sqlite3";
/* Load SheetJS library */
import * as XLSX from "xlsx";
import * as fs from "fs";
XLSX.set_fs(fs);
/* Initialize database */
var db = Database("chinook.db");
/* Create new workbook */
var wb = XLSX.utils.book_new();
/* Get list of table names */
var sql = db.prepare("SELECT name FROM sqlite_master WHERE type='table'");
var result = sql.all();
/* Loop across each name */
result.forEach(function(row) {
/* Get first 100K rows */
var aoo = db.prepare("SELECT * FROM '" + row.name + "' LIMIT 100000").all();
if(aoo.length > 0) {
/* Create Worksheet from the row objects */
var ws = XLSX.utils.json_to_sheet(aoo, {dense: true});
/* Add to Workbook */
XLSX.utils.book_append_sheet(wb, ws, row.name);
}
});
/* Write File */
XLSX.writeFile(wb, "SheetJSQLiteNode.xlsx");