Introducing the xlsx-kaku Library that Writes Excel Files in Node.js

motinados

motinados

Posted on March 31, 2024

Introducing the xlsx-kaku Library that Writes Excel Files in Node.js

As the developer of the Excel file output library xlsx-kaku, I am eager to directly share its features and advantages, as well as points to be mindful of. Released initially in December 2023, this library is sure to be of great assistance in generating Excel files, especially on the client side.

Small Build Size: Enhanced Client-Side Performance

One of the library's main highlights is its small build size, which is particularly suitable for client-side use, directly leading to shorter download times and improved execution speed. As developers, we always prioritize user experience and hope this library will aid in achieving that goal.

Below are images from building a project made with Vite + React.

This is before adding xlsx-kaku.
build size before adding xlsx-kaku

This is after adding xlsx-kaku, with the gzip size only increasing by about 20kB.

build size after adding xlsx-kaku

Dedicated to File Output: Simple Structure for High Reliability

The library specializes in Excel file output, meaning its simple structure allows developers to easily understand and utilize it. The focus on file output alone reduces the chances of bugs and enables swift debugging if issues arise.

Points of Caution

Possibility of API Changes

Since this library was first released in December 2023 and continues to evolve, there might be changes to the API in the future. This variability could affect the project-dependent code, so developers need to be aware of compatibility issues and track library updates.

Lack of Data Conversion Utilities

Moreover, data conversion utilities are not yet provided, which may necessitate writing additional code when using the library. This could potentially increase the development burden, especially in large projects or those with complex data structures.

How to Use

Before trying the following code, please install with the command npm install xlsx-kaku.

Basic Code

xlsx-kaku works on both the client-side and server-side.

import { Workbook } from "xlsx-kaku";

const wb = new Workbook();
const ws = wb.addWorksheet("Sheet1");

ws.setCell(0, 0, { type: "string", value: "Hello" });
ws.setCell(0, 1, { type: "number", value: 123 });

const xlsx = wb.generateXlsxSync();
Enter fullscreen mode Exit fullscreen mode

React Code

I turned the previous code into a React DownloadButton component.

import { Workbook } from "xlsx-kaku";

export default function DownloadButton() {
  const handleDownload = () => {
    const wb = new Workbook();
    const ws = wb.addWorksheet("Sheet1");

    ws.setCell(0, 0, { type: "string", value: "Hello" });
    ws.setCell(0, 1, { type: "number", value: 123 });

    const xlsx = wb.generateXlsxSync();

    const blob = new Blob([xlsx], {
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    });
    const url = URL.createObjectURL(blob);
    const link = document.createElement("a");
    link.href = url;
    link.download = "sample.xlsx";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    URL.revokeObjectURL(url);
  };

  return (
    <div>
      <button onClick={handleDownload}>download xlsx</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

As a developer, I hope this library will bring value to your projects, contributing to performance optimization and simplification of the development process. Moving forward, we will focus especially on providing data conversion utilities to make it an even more user-friendly library.

Related Links

💖 💪 🙅 🚩
motinados
motinados

Posted on March 31, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related