---
title: Packing Sheets with Rspack
sidebar_label: Rspack
pagination_prev: demos/index
pagination_next: demos/grid/index
sidebar_position: 18
---
import current from '/version.js';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import CodeBlock from '@theme/CodeBlock';
[Rspack](https://rspack.rs/) is a fast module bundler for JavaScript.
[SheetJS](https://sheetjs.com) is a JavaScript library for reading and writing
data from spreadsheets.
This demo uses Rspack and SheetJS to export data. We'll explore how to add
SheetJS to a site using Rspack and how to export data to spreadsheets.
:::note pass
This demo focuses on integration details with the Rspack bundler.
The demos follow the ["Export Tutorial"](/docs/getting-started/examples/export),
which covers SheetJS library usage in more detail.
:::
:::note Tested Deployments
This demo was tested in the following environments:
| Rspack | Date |
|:--------|:-----------|
| `1.6.3` | 2025-11-15 |
:::
## Integration Details
[The "Frameworks" section](/docs/getting-started/installation/frameworks) covers
installation with Yarn and other package managers.
After installing the SheetJS module in a Rspack project, `import` statements can
load relevant parts of the library.
Projects that import data will use methods such as `read`[^1] to parse workbooks
and `sheet_to_json`[^2] to generate usable data from files. As `sheet_to_json`
is part of the `utils` object, the required import is:
```js
import { read, utils } from 'xlsx';
```
Projects that export data will use methods such as `json_to_sheet`[^3] to
generate worksheets and `writeFile`[^4] to export files. As `json_to_sheet` is
part of the `utils` object, the required import is:
```js
import { utils, writeFile } from 'xlsx';
```
## Complete Example
This demo follows the [Export Example](/docs/getting-started/examples/export).
0) Initialize a new project:
```bash
mkdir sheetjs-rspack
cd sheetjs-rspack
npm init -y
```
1) Install the dependencies using a package manager:
{`\
npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz @rspack/core @rspack/cli`}
{`\
pnpm install --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz @rspack/core @rspack/cli`}
{`\
yarn add https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz @rspack/core @rspack/cli`}
2) Save the following to `src/index.js`:
```js title="src/index.js"
import { utils, writeFileXLSX } from 'xlsx';
document.getElementById("xport").addEventListener("click", async() => {
/* fetch JSON data and parse */
const url = "https://docs.sheetjs.com/executive.json";
const raw_data = await (await fetch(url)).json();
/* filter for the Presidents */
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
/* sort by first presidential term */
prez.forEach(row => row.start = row.terms.find(term => term.type === "prez").start);
prez.sort((l,r) => l.start.localeCompare(r.start));
/* flatten objects */
const rows = prez.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));
/* generate worksheet and workbook */
const worksheet = utils.json_to_sheet(rows);
const workbook = utils.book_new();
utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });
/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];
/* create an XLSX file and try to save to Presidents.xlsx */
writeFileXLSX(workbook, "Presidents.xlsx");
});
```
3) Create bundle:
```bash
npx -p @rspack/cli rspack build
```
:::caution pass
This build may fail with a module error:
```
ERROR in ./src/index.js
× Module parse failed:
╰─▶ × JavaScript parse error: 'import', and 'export' cannot be used outside of module code
```
Some versions of the `npm` tool create `package.json` files with the option
`"type": "commonjs"`. Rspack will fail if that option is specified. Edit
`package.json` and remove the property:
```js title="package.json (remove highlighted line)"
"license": "ISC",
// highlight-next-line
"type": "commonjs",
"dependencies": {
```
:::
This command will create the script `dist/main.js`
4) Create a small HTML page that loads the generated script:
```html title="index.html"
SheetJS Presidents Demo
```
5) Start a local HTTP server:
```bash
npx -y http-server .
```
Access the displayed URL (typically `http://localhost:8080`) with a web browser.
Click the "Click here to export" button to generate `Presidents.xlsx`
[^1]: See [`read` in "Reading Files"](/docs/api/parse-options)
[^2]: See [`sheet_to_json` in "Utilities"](/docs/api/utilities/array#array-output)
[^3]: See [`json_to_sheet` in "Utilities"](/docs/api/utilities/array#array-of-objects-input)
[^4]: See [`writeFile` in "Writing Files"](/docs/api/write-options)