npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-xlsxwriter

v0.1.7

Published

node-xlsxwriter is a Node.js wrapper for the Rust library rust_xlsxwriter

Downloads

696

Readme

Node-xlsxwriter

node-xlsxwriter is a Node.js wrapper for the Rust library rust_xlsxwriter.

It allows you to create Excel files in the .xlsx format,with a high level of performance,in a simple way.

Installation

npm install node-xlsxwriter

Usage

const { Workbook, Sheet, Format, Color, Border } = require('node-xlsxwriter');
/*
import * as xlsx from 'node-xlsxwriter';
const { Workbook, Sheet, Format, Color, Border } = xlsx;
for typescript/Es6
*/

const workbook = new Workbook();
const sheet = new Sheet('SomeSheet');
sheet.addColumnConfig({
  index: 0,
  size: {
    value: 30,
  },
});
sheet.addColumnConfig({
  index: 1,
  size: {
    value: 30,
  },
});
const format = new Format({
  align: 'center',
  bold: true,
  backgroundColor: new Color({ red: 255 }),
  fontSize: 16,
  underline: 'double',
  fontScheme: 'minor',
  fontName: 'Arial',
});

format.setBorder(new Border('thin', new Color()));
// const sheet = workbook.addSheet(); if you don't care about the name
sheet.writeString(0, 0, 'Hello');
sheet.writeNumber(1, 0, 123);
sheet.writeLink(2, 0, new Link('http://example.com', 'Example', 'tooltip'));
sheet.writeCell(3, 0, 'World', 'string');
sheet.writeString(0, 1, 'Hello', format);
workbook.pushSheet(sheet); // not necessary if you use addSheet

const buffer = workbook.saveToBufferSync();
const asyncBuffer = await workbook.saveToBuffer();
const path = 'path/to/file.xlsx';
const base64 = workbook.saveToBase64Sync();
workbook.saveToFileSync(path);

You also can use the writeFromJson method to create a sheet from a JSON object.

 const objects = [
    {
      name: 'John',
      age: 30,
      website: new Link('http://example.com', 'Example', 'tooltip'),
      date: new Date(),
    },
    {
      name: 'Jane',
      age: 25,
      website: new Link('http://example.com', 'Example', 'tooltip'),
      date: new Date(),
    },
  ];

  const workbook = new Workbook();
  const sheet = workbook.addSheet();
  sheet.writeFromJson(objects);

  const buffer = await workbook.saveToBuffer();

Obs: The writeFromJson method trades performance for convenience, so if are only generating the JSON for the sheet and not using anywhere else, it's better to use the writeCell method instead.

More complex examples

More complex examples can be found in the javascript/docs folder.

Why?

The main goal of this project was a solution of a problem that i was having with excel4node witch and latter xlsx.

I needed to take a bunch of data (50.000 rows) and put it in a simple Excel file, but the libraries mentioned above were taking too long or blocking my Event loop, excel4node was especially slow, xlsx was faster, but still blocking the event loop and i could't find a easy way to make it non-blocking.

No hate to these libraries, they are great, and much more complete,tested and used than this one, but they were not meeting my needs.

So i decided to create this project,using Rust to do the heavy lifting,using Neon i could easily spawn an async task that was faster for my use cases and non-blocking.

So if you need to generate simple Excel files with a high level of performance, this project is for you 😄.

rust_xlsxwriter offers a lot of features, but i'm still working on the API to expose all of them, so if you need a feature that is not implemented yet, please open an issue or a PR. There are a lot of room for improvement, on the performance too,especially on the serialization of the data, but i'm getting great results with this project. But for the simple stuff, it's already working.

Benchmarks

Every benchmark on run on a 100000ms time on this PC:

  • Linux 5.15.146.1-microsoft-standard-WSL2 x64
  • Node.JS: 20.12.2
  • V8: 11.3.244.8-node.19
  • CPU: AMD Ryzen 7 5800X 8-Core Processor × 16
  • Memory: 16 GB

100 rows x 7 columns: alt text

1000 rows x 7 columns: alt text

10_000 rows x 7 columns: alt text

Warnings ⚠️

Binary

The installation process will automatically download the ./native/node-xlsxwriter.node binary for your system, but if you have any problems, you can download it manually from the releases page.

Work in progress

Remember that this project is still in development, so some features may not work as expected.

Also, the API may change in the future.

Any help is welcome.

Format

Formats you can and should reuse them, because they are cached internally, so you can save memory and improve performance. The same goes for conditional formats.

Example:

don't do this

for (let i = 0; i < 100; i++) {
  const format = new Format();
  sheet.writeString(0, i, 'Hello', format);
}

do this

const format = new Format();
for (let i = 0; i < 100; i++) {
  sheet.writeString(0, i, 'Hello', format);
}

The impact is not noticeable in small files, but it can be significant in large files.

Building from source

If you want to build the project from source, you need to have Rust installed on your machine.

git clone [email protected]:VictorRibeiroLima/node-xlsxwriter.git
cd node-xlsxwriter
npm install
npm run release:native

This will generate the ./native/node-xlsxwriter.node binary for your system.