@betteroffice/xlsx

中文文档
0.5.1发布于 2026/10/27Apache-2.0TypeScriptES Module零依赖构建来源已验证近 30 天下载 36.3万

Excel-faithful XLSX spreadsheet editor and viewer core: full OOXML fidelity, real-time human + AI agent collaboration. Framework-free (Rust/Wasm).

原始使用文档
文档目录13 节
  1. Install
  2. Usage
  3. Entry points
  4. Canvas rendering and grid helpers
  5. Range moves
  6. Printing and PNG export
  7. Worker sessions
  8. Edit proposals
  9. Version-checked edit batches
  10. Structured export
  11. Collaboration
  12. Operation profiles
  13. Documentation

@betteroffice/xlsx

Framework-free XLSX parsing, formula calculation, editing, and rendering through a Rust/WebAssembly engine. Includes canvas painting, viewport, hit-testing, accessibility, and clipboard helpers.

Install

bun add @betteroffice/xlsx

No peer dependencies.

Usage

Sheet, row and column indices are zero-based, so editCell(0, 9, 2, ...) writes cell C10 on the first sheet.

import { initWasm, openWorkbook } from '@betteroffice/xlsx';

export async function updateWorkbook(bytes: Uint8Array): Promise<Uint8Array> {
  await initWasm();
  const workbook = openWorkbook(bytes);
  try {
    workbook.editCell(0, 9, 2, '=SUM(C1:C9)');
    return workbook.save();
  } finally {
    workbook.dispose();
  }
}

Cell input parses text, numbers, booleans, and formulas; formulas start with =. Editing a cell recalculates its dependents.

Entry points

These entry points are exported from @betteroffice/xlsx:

entry pointdescription
initWasm(input?)Initialize the engine; concurrent calls share initialization.
openWorkbook(bytes, options?)Open XLSX bytes as a synchronous WorkbookHandle after initialization.
openWorkbookSession(bytes, options?)Open a workbook in a dedicated worker with asynchronous access.
paintDisplayList(ctx, frame, dpr, origin?)Paint a display list onto a canvas 2D context.
isPngExportAvailable()Check whether the initialized engine supports PNG export.
isProposalsAvailable()Check whether the initialized engine supports edit proposals.
exportXlsxStructured(bytes, options?)Asynchronously export stored workbook content from bytes.
exportXlsxMarkdown(bytes, options?, markdownOptions?)Asynchronously export Markdown from bytes.

initWasm() loads the packaged WASM asset by default. Pass WASM bytes or a compiled WebAssembly.Module in runtimes that cannot fetch that URL. Dispose workbook handles when finished.

Canvas rendering and grid helpers

WorkbookHandle.displayList(viewport) returns the active sheet's display list. Size the backing store for the device pixel ratio and give the canvas its logical CSS dimensions:

import { paintDisplayList, type WorkbookHandle } from '@betteroffice/xlsx';

export function drawWorkbook(workbook: WorkbookHandle, canvas: HTMLCanvasElement) {
  const dpr = window.devicePixelRatio || 1;
  canvas.width = 800 * dpr;
  canvas.height = 600 * dpr;
  canvas.style.width = '800px';
  canvas.style.height = '600px';
  const ctx = canvas.getContext('2d');
  if (!ctx) throw new Error('Canvas 2D is unavailable');
  const frame = workbook.displayList({ x: 0, y: 0, width: 800, height: 600 });
  paintDisplayList(ctx, frame, dpr);
}

For a custom grid, use cellAtPoint, cellRect, and rangeRect for hit-testing and geometry, buildA11yGrid for accessibility data, and toTsv / fromTsv for clipboard text. The @betteroffice/xlsx/headless entry exports pure grid helpers and types without the canvas painter or WASM loader.

Range moves

workbook.moveRange(sheet, source, destination) moves an A1 range within a sheet and rewrites references in one undo step; the destination is the new top-left cell. Surface errors when a move is refused rather than discarding the source workbook.

Printing and PNG export

workbook.printDisplayList(sheet, range, metrics, gridlines) renders an A1 range using PrintMetrics measured from the workbook's normal font. The caller chooses paper size, margins, and pagination; paintDisplayList accepts an optional origin in backing-store pixels for positioning the result.

workbook.renderPng(viewport) exports the active sheet's viewport. workbook.renderRangePng() exports its used range, or accepts { range, scale } to select an A1 range and raster scale. Both return PNG bytes synchronously; check isPngExportAvailable() before calling them.

Worker sessions

The low-level openWorkbookSession API (also exported from @betteroffice/xlsx/worker) is experimental. It returns a WorkbookSession with promise-returning operations through session.call, plus asynchronous session.save() and session.dispose() methods.

The React editor uses a worker by default and exposes its own asynchronous API. Use @betteroffice/xlsx-react for the editor UI.

Edit proposals

workbook.propose(agentId, note, edits) stages cell edits for review without applying them. listProposals, acceptProposal, and rejectProposal manage that queue; acceptance applies the proposal as one undo step and recalculates dependents. Check isProposalsAvailable() first; acceptance throws StaleProposalError if targeted cells have changed since staging.

Version-checked edit batches

readCells returns cells with their version; validateEdits checks a batch without committing it, and applyEdits applies all steps or returns a typed refusal without changing the workbook. See Edit XLSX in version-checked batches for targets, guards, supported steps, and limits.

Structured export

exportStructured and exportMarkdown read committed content with positional anchors and diagnostics without recalculating formulas. See Export XLSX content with anchors for scopes, hidden content, limits, and byte-based exports.

Collaboration

The @betteroffice/xlsx/collaboration entry exports CollaborationProvider for connecting replicas to a binary transport. See the collaboration guide for provider lifecycle, transport setup, and shared starting state.

Open the workbook with openWorkbook(bytes, { collaborative: true }). Collaborative workbooks synchronize cell content, formulas, styles, column widths and row heights, with local undo and redo whose effects synchronize. Structural edits throw: inserting or deleting rows and columns, adding, removing, moving or renaming sheets, and changing charts or defined names.

Operation profiles

editCellProfiled, applyOpsProfiled, and displayListProfiled return engine stage durations in milliseconds alongside their results. See Operation profiles.

Documentation

版本与发布记录

@betteroffice/xlsx 共发布 14 个版本,首次发布于 2026/07/13;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.1latest2026/10/2712 天前
0.5.02026/10/2613 天前
0.3.02026/09/291 个月前
0.2.12026/09/171 个月前
0.2.02026/09/161 个月前
0.1.02026/08/202 个月前

关于 @betteroffice/xlsx 的常见问题

@betteroffice/xlsx 是什么?

Excel-faithful XLSX spreadsheet editor and viewer core: full OOXML fidelity, real-time human + AI agent collaboration. Framework-free (Rust/Wasm).

如何安装 @betteroffice/xlsx?

在项目目录运行 npm install @betteroffice/xlsx,或使用 pnpm add / yarn add / bun add。

@betteroffice/xlsx 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@betteroffice/xlsx 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@betteroffice/xlsx 可以商用吗?

@betteroffice/xlsx 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@betteroffice/xlsx 的下载量有多少?

根据 npm 官方下载统计,@betteroffice/xlsx 在 2026/10/08 – 2026/11/06 这 30 天内被下载 363,474 次,日均约 12,116 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@betteroffice/xlsx 还在维护吗?

最新版本 0.5.1 发布于 2026/10/27(12 天前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/07/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。