@betteroffice/xlsx-react

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

Excel-faithful React XLSX spreadsheet editor and viewer: full OOXML fidelity, real-time human + AI agent collaboration.

原始使用文档
文档目录10 节
  1. Install
  2. Usage
  3. Key options
  4. Editor API
  5. Saving
  6. Toolbar
  7. Plugins and edit batches
  8. Structured export
  9. Collaboration
  10. Documentation

@betteroffice/xlsx-react

React XLSX editor with a canvas grid, toolbar, selection, keyboard input, and clipboard support. The engine runs in a Web Worker by default, with promise-returning host operations and exports.

Install

bun add @betteroffice/xlsx-react react react-dom

react and react-dom 18 or 19 are peer dependencies; the XLSX core is included as a dependency.

Usage

import { XlsxEditor } from '@betteroffice/xlsx-react';

export function Spreadsheet({ bytes }: { bytes: Uint8Array }) {
  return (
    <div style={{ height: 600 }}>
      <XlsxEditor file={bytes} fileName="report.xlsx" />
    </div>
  );
}

file contains XLSX bytes. Give the editor's container a height; omitting file shows an empty editor that asks for an XLSX file. See hosting for worker, chunk and CSP needs.

Key options

The default props are described by XlsxAsyncEditorProps:

proptypedescription
fileUint8Array | ArrayBufferXLSX bytes to open.
fileNamestringDownload name; defaults to workbook.xlsx.
onReady(api: XlsxAsyncEditorApi) => void | (() => void)Receive the editor API; optionally return cleanup.
onSave(bytes: Uint8Array) => voidReceive saved workbook bytes.
onSaveRequest() => boolean | void | Promise<boolean | void>Intercept saves; return true to continue.
downloadOnSavebooleanSave also downloads; defaults to !onSave.
onDocumentChange(change: XlsxDocumentChange) => voidReceives { version } after a committed edit.
initialZoomnumberInitial scale, clamped to 0.1 through 5; defaults to 1.
loadingIndicatorReact.ReactNodeShown while a supplied file opens.
styleReact.CSSPropertiesOverrides root inline styles.
onChange() => voidCalled after a user edit changes the workbook.
onError(error: Error) => voidReceive engine errors.
readOnlybooleanBlock user edits while keeping navigation and selection.
toolbarReact.ReactNodeReplace the default toolbar; null removes it.
showToolbarbooleanfalse hides the toolbar region, including custom content.
collaborationXlsxEditorCollaborationOptionsConfigure the editor-owned replica and presence provider.
i18nTranslationsOverride UI strings using @betteroffice/xlsx-i18n.
pluginsreadonly XlsxPlugin[]Install host plugins.
pluginGrantsReadonly<Record<string, XlsxPluginGrant>>Grant capabilities by plugin ID.
onPluginError(error: XlsxPluginError) => voidReceive plugin failures.
classNamestringCSS class for the editor root.

All listed props are optional. The default toolbar is hidden when readOnly; a supplied toolbar remains visible unless showToolbar is false.

Deprecated: legacySyncApi keeps the synchronous API on the in-thread engine and is removed in 0.6.0; omit it for the default asynchronous editor API.

Editor API

onReady receives XlsxAsyncEditorApi. Its exports wait for pending editor input before reading the workbook, and its command store is shared with the toolbar and host controls.

memberpurpose
hydratedWhether the worker editor is ready.
whenHydrated()Await editor readiness.
failureCurrent engine failure, or null.
commandsInspect command availability and execute actions asynchronously.
version(), readCells, findTextRead after pending input commits.
validateEdits, applyEditsValidate or apply version-checked batches; refuse while read-only.
save()Commit input, return bytes and call onSave without downloading or consulting onSaveRequest.
focus()Focus the editor.
renderPng, renderRangePng, printDisplayListRender asynchronously.
exportStructured, exportMarkdownExport content asynchronously.
proposeChangesOptional method for staging tracked cell edits on worker editors.
collaborationAsynchronous collaboration replica, or null.
recoverySave()Recover bytes after a non-collaborative worker session fails.

Treat the API as belonging to the open document. Return cleanup from onReady to release host subscriptions when that document is replaced or the editor unmounts.

Saving

Use await api.commands.execute('save', null) to save from host UI. The command waits for accepted input and calls onSave when supplied. It also downloads when downloadOnSave ?? !onSave is true. Use await api.save() to receive bytes directly without downloading or consulting onSaveRequest.

onSaveRequest intercepts toolbar saves, the save command, and Ctrl/Cmd+S before serialization. Return true to continue built-in saving; false or void handles or cancels the request. Promises are awaited, concurrent requests are coalesced, and requests for replaced documents are discarded.

Commands return a success or a typed failure. A refused cell draft must be corrected or discarded before saving can complete. recoverySave() requires a failed non-collaborative worker session and returns { bytes, recovery: true }.

Toolbar

The toolbar composition API is experimental. Use toolbar to supply controls connected to the editor's command store. See Compose the XLSX toolbar for built-in parts, custom controls, and command results.

Plugins and edit batches

The plugin API is experimental and may change in minor releases. Plugins contribute panels, overlays, and commands through restricted clients; their read and edit clients support version-checked operations. See Host XLSX editor plugins and Edit XLSX in version-checked batches.

Structured export

Await api.exportStructured() or api.exportMarkdown() for content with positional anchors and omission diagnostics. See Export XLSX content with anchors for scopes, hidden content, and export limits.

Collaboration

Pass collaboration and connect a provider when collaboration.onReplica publishes the replica. The editor retains its default worker runtime; see the collaboration guide for transport setup and cleanup.

Documentation

版本与发布记录

@betteroffice/xlsx-react 共发布 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-react 的常见问题

@betteroffice/xlsx-react 是什么?

Excel-faithful React XLSX spreadsheet editor and viewer: full OOXML fidelity, real-time human + AI agent collaboration.

如何安装 @betteroffice/xlsx-react?

在项目目录运行 npm install @betteroffice/xlsx-react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)、react-dom(^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@betteroffice/xlsx-react 支持 TypeScript 吗?

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

@betteroffice/xlsx-react 支持 ESM 还是 CommonJS?

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

@betteroffice/xlsx-react 可以商用吗?

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

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

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

@betteroffice/xlsx-react 还在维护吗?

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