Getting Started
Create template
Basic part of using XLSX-Renderer is creating a template file.
- Open MS Excel (or another worksheet manager) and create new file,
- In
A1
type# Hello ${name}! How are you?
- which insert variablename
into template string, - In
A2
type#! END_ROW
- which commits this row and move to the next one, - In
B1
typeTotal items:
, - In
B2
type## items.length
- which writes length of arrayitems
, - In
B3
type#! FINISH
- which tells to the renderer, that should finish rendering of this worksheet. - Save file as
report-template.xlsx
in your project dir, - (optional) Try some various of styling, widths, heights and ect.
Write a code
NodeJS:
import {Renderer} from 'xlsx-renderer';
// ... define viewModel
const viewModel = { name:"World", items:[/*...*/] };
// ... generate a report:
const renderer = new Renderer();
const result = await renderer.renderFromFile('./report-template.xlsx', viewModel);
await result.xlsx.writeFile('./my-awesome-report.xlsx');
Or for browser:
import { Renderer } from "xlsx-renderer";
import { saveAs } from "file-saver";
// ... define viewModel:
const viewModel = { name:"World", items:[/*...*/] };
//... generate a report:
// 1. Download a template.
fetch("./template.xlsx")
// 2. Get template as ArrayBuffer.
.then((response) => response.arrayBuffer())
// 3. Fill the template with data (generate a report).
.then((buffer) => new Renderer().renderFromArrayBuffer(buffer, viewModel))
// 4. Get a report as buffer.
.then((report) => report.xlsx.writeBuffer())
// 5. Use `saveAs` to download on browser site.
.then((buffer) => saveAs(new Blob([buffer]), `${Date.now()}_report.xlsx`))
// Handle errors.
.catch((err) => console.log("Error writing excel export", err));
What's next?
I recommend to study examples and deeply analyze the Cells documentation.
Useful links:
- Cells commands - describes how different values for cells drive the renderer.
- Examples - list of set of template, viewModel and expected results used for testing purposes.
- Discussions - There are some topics, where people ask how to do some goals.
- Gitter - community live chat.
- CLI - Command line interface, handy for creating and testing templates.