Skip to main content

Getting Started

Create template

Basic part of using XLSX-Renderer is creating a template file.

  1. Open MS Excel (or another worksheet manager) and create new file,
  2. In A1 type # Hello ${name}! How are you? - which insert variable name into template string,
  3. In A2 type #! END_ROW - which commits this row and move to the next one,
  4. In B1 type Total items:,
  5. In B2 type ## items.length - which writes length of array items,
  6. In B3 type #! FINISH - which tells to the renderer, that should finish rendering of this worksheet.
  7. Save file as report-template.xlsx in your project dir,
  8. (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.

  • 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.