Skip to main content

独立浏览器脚本

每个独立的发布脚本均可在 https://cdn.sheetjs.com/ 处获得。

¥Each standalone release script is available at https://cdn.sheetjs.com/.

The current version is 0.20.3 and can be referenced as follows:

<!-- use version 0.20.3 -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js"></script>

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

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

许多服务托管旧版本的 SheetJS 库。由于同步问题,它们通常已经过时。

¥A number of services host older versions of the SheetJS libraries. Due to syncing issues, they are generally out of date.

SheetJS CDN https://cdn.sheetjs.com/ 是 SheetJS 脚本的权威来源

¥The SheetJS CDN https://cdn.sheetjs.com/ is the authoritative source for SheetJS scripts

浏览器脚本

¥Browser Scripts

xlsx.full.min.js 是完整的独立脚本。它包括对读取和写入许多电子表格格式的支持。

¥xlsx.full.min.js is the complete standalone script. It includes support for reading and writing many spreadsheet formats.

<!-- use xlsx.full.min.js from version 0.20.3 -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js"></script>

xlsx.mini.min.js 是一个更精简的版本,省略了以下功能:

¥xlsx.mini.min.js is a slimmer build that omits the following features:

  • CSV 和 SYLK 编码(直接影响美国以外的用户)

    ¥CSV and SYLK encodings (directly affecting users outside of the United States)

  • XLSB / XLS / Lotus 1-2-3 / SpreadsheetML 2003 / Numbers file formats

  • 流实用函数

    ¥Stream utility functions

How to integrate the mini build (click to show)

Replace references to xlsx.full.min.js with xlsx.mini.min.js. Starting from scratch, a single script tag should be added at the top of the HTML page:

<!-- use xlsx.mini.min.js from version 0.20.3 -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.mini.min.js"></script>

¥Vendoring

为了总体稳定性,强烈建议制作 SheetJS 脚本 ("vendoring") 的本地副本。供应将网站与 SheetJS 基础设施解耦。

¥For general stability, making a local copy of SheetJS scripts ("vendoring") is strongly recommended. Vendoring decouples websites from SheetJS infrastructure.

  1. Download the script (xlsx.full.min.js) for the desired version. The current version is available at https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js

  1. 将脚本与其他脚本一起移至 public 文件夹。

    ¥Move the script to a public folder with other scripts.

  2. 从 HTML 页面引用本地脚本:

    ¥Reference the local script from HTML pages:

<script src="/public/xlsx.full.min.js"></script>

该脚本分配给 window.XLSX。全局可以在其他脚本中使用。

¥This script assigns to window.XLSX. The global can be used in other scripts.

Internet Explorer 和旧版浏览器

¥Internet Explorer and Older Browsers

为了与 JavaScript 引擎广泛兼容,该库是使用 ECMAScript 3 语言方言编写的。"shim" 脚本提供了针对旧浏览器和环境的功能实现。

¥For broad compatibility with JavaScript engines, the library is written using ECMAScript 3 language dialect. A "shim" script provides implementations of functions for older browsers and environments.

由于 SSL 兼容性问题,旧版本的 IE 将无法直接使用 CDN 脚本。它们应该下载并保存到公共路径:

¥Due to SSL compatibility issues, older versions of IE will not be able to use the CDN scripts directly. They should be downloaded and saved to a public path:

必须在独立脚本之前添加对垫片的 script 引用:

¥A script reference to the shim must be added before the standalone script:

<!-- add the shim first -->
<script type="text/javascript" src="shim.min.js"></script>
<!-- after the shim is referenced, add the library -->
<script type="text/javascript" src="xlsx.full.min.js"></script>

Web Worker

可以使用工作脚本顶部的 importScripts 加载独立脚本:

¥The standalone scripts can be loaded using importScripts at the top of the worker scripts:

importScripts("https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/shim.min.js");
importScripts("https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js");

类型检查器

¥Type Checker

VSCode 遥测和数据泄露

Visual Studio Code ("VSCode") 的官方 Microsoft 版本嵌入遥测并将信息发送到外部服务器。

¥The official Microsoft builds of Visual Studio Code ("VSCode") embed telemetry and send information to external servers.

VSCodium 是 VSCode 的一个无遥测分支。

¥VSCodium is a telemetry-free fork of VSCode.

在编写可能处理个人身份信息 (PII) 的代码时,SheetJS 团队强烈建议从源代码构建 VSCode 或使用不会泄露数据的 IDE。

¥When writing code that may process personally identifiable information (PII), the SheetJS team strongly encourages building VSCode from source or using IDEs that do not exfiltrate data.

VSCodium 和 VSCode 中集成的类型检查器在使用独立构建时目前不提供类型提示。使用 JSDoc @type 指令与类型导入相结合,VSCodium 将识别类型:

¥The type checker integrated in VSCodium and VSCode do not currently provide type hints when using the standalone build. Using the JSDoc @type directive coupled with type imports, VSCodium will recognize the types:

VSCodium types

  1. Download the types (index.d.ts) for the desired version. The current version is available at https://cdn.sheetjs.com/xlsx-0.20.3/package/types/index.d.ts

  1. 将类型文件重命名为 xlsx.d.ts。它不需要与独立脚本位于同一文件夹中。

    ¥Rename the types file to xlsx.d.ts. It does not need to reside in the same folder as the standalone script.

  2. 在引用全局的浏览器脚本中,添加以下行:

    ¥In the browser script referencing the global, prepend the following lines:

Prepend this fragment in each source file referencing the XLSX global
/** @type {import("./xlsx")} */
const XLSX = globalThis.XLSX;
  1. 如果 xlsx.d.ts 文件位于不同的文件夹中,请将参数更改为 import 方法以反映相对路径。例如,给定结构:

    ¥If the xlsx.d.ts file is in a different folder, change the argument to the import method to reflect the relative path. For example, given the structure:

Folder Structure
- /vendor
- /vendor/xlsx.ts
- /src
- /src/app.js

/src/app.js 必须将类型引用为 ../vendor/xlsx

¥/src/app.js must refer to the types as ../vendor/xlsx:

Preamble for /src/app.js when types are at /vendor/xlsx.d.ts
/** @type {import("../vendor/xlsx")} */
const XLSX = globalThis.XLSX;

必须省略 .d.ts 文件扩展名。

¥The .d.ts file extension must be omitted.

使用 @import 指令的 JSDoc 类型在 <script> 标签中不受支持。

¥JSDoc types using the @import directive are not supported in <script> tags.

这是 VSCode 的一个已知错误!

¥This is a known bug with VSCode!

ECMAScript 模块导入

¥ECMAScript Module Imports

本节涉及使用 <script type="module"> 的 HTML 页面中的导入。

¥This section refers to imports in HTML pages using <script type="module">.

"框架和打包器" 部分涵盖了使用打包器 (ViteJS) 或框架 (Kaioken / ReactJS / Angular / VueJS / Svelte) 的项目中的导入

¥The "Frameworks and Bundlers" section covers imports in projects using bundlers (ViteJS) or frameworks (Kaioken / ReactJS / Angular / VueJS / Svelte)

ECMAScript 模块构建保存到 xlsx.mjs,可以使用 type="module" 直接添加到带有 script 标记的页面:

¥The ECMAScript Module build is saved to xlsx.mjs and can be directly added to a page with a script tag using type="module":

<script type="module">
import { read, writeFileXLSX } from "https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs";
</script>

如果需要编码支持,则必须手动导入 cpexcel.full.mjs

¥If Encoding support is required, cpexcel.full.mjs must be manually imported:

<script type="module">
/* load the codepage support library for extended support with older formats */
import { set_cptable } from "https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs";
import * as cptable from 'https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/cpexcel.full.mjs';
set_cptable(cptable);
</script>

Web Worker 支持在 "Web Worker" 演示 中注明

¥Web Worker support is noted in the "Web Workers" demo

动态导入

¥Dynamic Imports

import() 的动态导入只会在使用时下载 SheetJS 脚本。此示例将在导出数据时下载库:

¥Dynamic imports with import() will only download the SheetJS scripts when they are used. This example will download the library when data is exported:

<button id="xport">Export</button>
<script type="module">
xport.addEventListener("click", async() => {

/* dynamically import the script in the event listener */
const XLSX = await import("https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs");

const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet([["a","b","c"],[1,2,3]]);
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "SheetJSESMTest.xlsx");
});
</script>

回调函数必须标记为 async,脚本块必须具有属性 type="module"

¥The callback functions must be marked as async and the script block must have the attribute type="module"

如果需要编码支持,则必须手动导入 cpexcel.full.mjs

¥If Encoding support is required, cpexcel.full.mjs must be manually imported:

<button id="xport">Export</button>
<script type="module">
xport.addEventListener("click", async() => {

/* dynamically import the scripts in the event listener */
const XLSX = await import("https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs");
const cptable = await import("https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/cpexcel.full.mjs");
XLSX.set_cptable(cptable);

const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet([["a","b","c"],[1,2,3]]);
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "SheetJSESMTest.xlsx");
});
</script>

Bower

Bower 已被弃用,维护者建议使用其他工具。

¥Bower is deprecated and the maintainers recommend using other tools.

Bower 包管理器支持来自 SheetJS CDN 的 tarball:

¥The Bower package manager supports tarballs from the SheetJS CDN:

npx bower install https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz

Bower 将把独立脚本放在 bower_components/js-xlsx/dist/

¥Bower will place the standalone scripts in bower_components/js-xlsx/dist/