/json-xls-converter

Node utility to convert json to an Excel file.

Primary LanguageJavaScriptMIT LicenseMIT

json-xls-converter

npm npm GitHub license npm

Utility to convert json to an Excel file.

This is an updated version of json2xls which seems to be abandoned, and have some important vulnerabilities in its dependencies (some of them abandoned too).

This project is based in:

None of those projects included any kind of license, I'm distributing this new version with the MIT license.

Installation

npm i json-xls-converter

Usage

To save as a file:

import { converter } from 'json-xls-converter';
import fs from 'fs/promises';

const json = {
  foo: 'bar',
  qux: 'moo',
  poo: 123,
  stux: new Date()
}

const xls = await converter(json);

await fs.writeFile('data.xlsx', xls, 'binary');

Or use as an express middleware. It adds a convenience xls method to the response object to immediately output an excel file as a download.

const jsonArr = [{
  foo: 'bar',
  qux: 'moo',
  poo: 123,
  stux: new Date()
},
{
  foo: 'bar',
  qux: 'moo',
  poo: 345,
  stux: new Date()
}];

app.use(converter.middleware);

app.get('/', (req, res) => {
  res.xls('data.xlsx', jsonArr);
});

Migrating from json2xls to json-xls-converter

  1. npm remove json2xls && npm install json-xls-converter

  2. Change every import from:

    import json2xls from 'json2xls';

    to:

    import { converter } from 'json-xls-converter';

  3. Change every invocation from:

    const xls = json2xls(data);

    to:

    const xls = await converter(data);

  4. If you are using it as an express middleware, change from:

    app.use(json2xls.middleware);

    to:

    app.use(converter.middleware);