Skip to main content

Bun

软件包 tarball 在 https://cdn.sheetjs.com 上可用。

¥Package tarballs are available on https://cdn.sheetjs.com.

https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz is the URL for version 0.20.3

Bun 支持被认为是实验性的。

优秀的开源软件会随着用户测试和报告而不断成长。任何问题都应报告给 Bun 项目以进行进一步诊断。

¥Great open source software grows with user tests and reports. Any issues should be reported to the Bun project for further diagnosis.

安装

¥Installation

Tarball 可以直接使用 bun install 安装:

¥Tarballs can be directly installed with bun install:

bun rm xlsx
bun install https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz

监视存储库 或订阅 RSS 订阅 以在新版本发布时收到通知!

¥Watch the repo or subscribe to the RSS feed to be notified when new versions are released!

¥Vendoring

为了总体稳定性,推荐使用 "vendoring" 模块:

¥For general stability, "vendoring" modules is the recommended approach:

  1. 删除对名为 xlsx 的项目的任何现有依赖:

    ¥Remove any existing dependency on a project named xlsx:

bun rm xlsx
  1. Download the tarball (xlsx-0.20.3.tgz) for the desired version. The current version is available at https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz

  1. 在项目的根目录下创建 vendor 子文件夹,并将 tarball 移至该文件夹。将其添加到你的项目存储库中。

    ¥Create a vendor subfolder at the root of your project and move the tarball to that folder. Add it to your project repository.

  2. 安装压缩包:

    ¥Install the tarball:

bun install file:vendor/xlsx-0.20.3.tgz

该软件包将作为 xlsx 安装和访问。

¥The package will be installed and accessible as xlsx.

用法

¥Usage

该软件包支持 CommonJS require 和 ESM import 模块系统。

¥The package supports CommonJS require and ESM import module systems.

强烈建议在 Bun 中使用 CommonJS。

¥It is strongly recommended to use CommonJS in Bun.

CommonJS require

默认情况下,模块支持 require,它将自动添加对编码、流和文件系统访问的支持:

¥By default, the module supports require and it will automatically add support for encodings, streams and file system access:

const { readFile } = require("xlsx");
const wb = readFile("pres.numbers"); // works!

在 BunJS REPL 中,require 错误地加载了 ESM 版本。

¥In the BunJS REPL, require incorrectly loads the ESM build.

ESM import

使用 ESM import 语句导入库时,不会加载原生 NodeJS 模块。它们必须手动添加:

¥When importing the library using ESM import statements, the native NodeJS modules are not loaded. They must be added manually:

import * as XLSX from 'xlsx';

/* load 'fs' for readFile and writeFile support */
import * as fs from 'fs';
XLSX.set_fs(fs);

/* load 'stream' for stream support */
import { Readable } from 'stream';
XLSX.stream.set_readable(Readable);

/* load the codepage support library for extended support with older formats */
import * as cpexcel from 'xlsx/dist/cpexcel.full.mjs';
XLSX.set_cptable(cpexcel);

打包

¥Bundling

对于服务器端脚本,bun build 可以预先优化依赖。Bun 构建器需要一个包含 SheetJS 依赖的正确 package.json

¥For server-side scripts, bun build can pre-optimize dependencies. The Bun builder requires a proper package.json that includes the SheetJS dependency.

测试部署

该演示最后在以下部署中进行了测试:

¥This demo was last tested in the following deployments:

架构BunJS日期
darwin-x641.1.42024-04-19
darwin-arm1.1.102024-09-22
win10-x641.1.42024-04-19
win11-x641.1.222024-08-11
linux-x641.1.42024-04-25
  1. 创建一个新项目:

    ¥Create a new project:

mkdir sheetjs-bun-dle
cd sheetjs-bun-dle
echo "{}" > package.json
PowerShell 编码错误

PowerShell 文件重定向将使用 UTF-16 LE 编码。Bun 不支持编码,将无法安装包:

¥The PowerShell file redirect will use the UTF-16 LE encoding. Bun does not support the encoding and will fail to install the package:

bun add v1.1.22-canary.96 (df33f2b2)
1 | ��{}

^
error: Unexpected ��

文件必须以 UTF8(无 BOM)或 ASCII 格式重新保存。

¥The file must be resaved in UTF8 (without BOM) or ASCII.

  1. 在 VSCodium 中打开 package.json

    ¥Open package.json in VSCodium.

当前编码显示在右下角:

¥The current encoding is displayed in the lower-right corner:

VSCodium status bar

  1. 单击显示的编码。

    ¥Click the displayed encoding.

  2. 在 "选择操作" 弹出窗口中,选择 "使用编码保存"

    ¥In the "Select Action" popup, select "Save with Encoding"

  3. 在新列表中,选择 UTF-8 utf8

    ¥In the new list, select UTF-8 utf8:

VSCodium encoding

VSCodium 将自动重新保存文件。

¥VSCodium will automatically re-save the file.

  1. 安装 SheetJS 包 tarball:

    ¥Install the SheetJS package tarball:

bun install https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
  1. 将以下脚本保存到 SheetJSBun.js

    ¥Save the following script to SheetJSBun.js:

SheetJSBun.js
import * as XLSX from 'xlsx';
import * as fs from 'fs';
XLSX.set_fs(fs);

/* fetch JSON data and parse */
const url = "https://xlsx.nodejs.cn/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 = XLSX.utils.json_to_sheet(rows);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Dates");

/* fix headers */
XLSX.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 */
XLSX.writeFile(workbook, "Presidents.xlsx");
  1. 将脚本与 bun build 打包在一起:

    ¥Bundle the script with bun build:

bun build --target=bun SheetJSBun.js --outfile=app.js

此过程将生成 app.js

¥This procedure will generate app.js.

  1. 删除模块工件和原始脚本:

    ¥Remove the module artifacts and original script:

rm package.json bun.lockb SheetJSBun.js
rm -rf ./node_modules

PowerShell 不支持 rm -rf。相反,必须删除每个文件:

¥PowerShell does not support rm -rf. Instead, each file must be removed:

Windows Powershell commands
rm package.json
rm bun.lockb
rm SheetJSBun.js
rm .\\node_modules -r -fo

此时,app.js 将成为项目文件夹中的唯一文件。

¥At this point, app.js will be the only file in the project folder.

  1. 运行脚本:

    ¥Run the script:

bun app.js

如果脚本成功,将创建文件 Presidents.xlsx。该文件可以在电子表格编辑器中打开。

¥If the script succeeded, the file Presidents.xlsx will be created. That file can be opened in a spreadsheet editor.