Excel4Node is pretty nice, I've found this to be very useful for creating Excel documents, the API is very flexible, it's easy to write objects / arrays to a file, style it and save.
Styling is very simple, you can create Style objects and apply them to cells, again this is very flexible.
For example:
const excel = require("excel4node");
const workbook = new excel.Workbook();
const style = workbook.createStyle({
font: { color: "#0101FF", size: 11 }
});
const worksheet = workbook.addWorksheet("Sheet 1");
const arrayToWrite = Array.from({length: 10}, (v, k) => [`Row ${k+1}, Col 1`,`Row ${k+1}, Col 2`]);
arrayToWrite.forEach((row, rowIndex) => {
row.forEach((entry, colIndex) => {
worksheet.cell(rowIndex + 1, colIndex + 1).string(entry).style(style);
})
})
workbook.write("text.xlsx");
There is also Xlsx, though I find this more useful for parsing than for file creation. It will also work in a browser.
To create a spreadsheet in memory, then allow the client to download:
const express = require("express");
const port = 8000;
const app = express();
const stream = require("stream");
const excel = require("excel4node");
function createTestWorkbook() {
const workbook = new excel.Workbook();
const style = workbook.createStyle({
font: { color: "#0101FF", size: 11 }
});
const worksheet = workbook.addWorksheet("Sheet 1");
const arrayToWrite = Array.from({length: 10}, (v, k) => [`Row ${k+1}, Col 1`,`Row ${k+1}, Col 2`]);
arrayToWrite.forEach((row, rowIndex) => {
row.forEach((entry, colIndex) => {
worksheet.cell(rowIndex + 1, colIndex + 1).string(entry).style(style);
})
})
return workbook;
}
/* Allow client to download spreadsheet. */
app.get('/get-spreadsheet', (req, res) => {
let workbook = createTestWorkbook();
workbook.write('workbook.xlsx', res);
});
app.listen(port);
console.log(`Serving at http://localhost:${port}`);
This requires you to do
npm install express
Then run the script and goto http://localhost:8000/get-spreadsheet/