Skip to main content

Standalone Browser Scripts独立浏览器脚本

Each standalone release script is available at https://cdn.sheetjs.com/.每个独立版本脚本都可以在https://cdn.sheetjs.com/上获得。

The current version is 0.18.9 and can be referenced as follows:当前版本为0.18.9,可用以如方式引用它:
<!-- use version 0.18.9 -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.18.9/package/dist/xlsx.full.min.js"></script>

The latest tag references the latest version and updates with each release:latest标签引用了最新版本和每个版本的更新:

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

Browser Scripts浏览器脚本

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.18.9 -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.18.9/package/dist/xlsx.full.min.js"></script>

A slimmer build is generated at dist/xlsx.mini.min.js. dist/xlsx.mini.min.js上生成了更瘦的构建。Compared to full build:与完整版本相比:

  • codepage library skipped (no support for XLS encodings)跳过了代码页库(不支持XLS编码)
  • no support for XLSB / XLS / Lotus 1-2-3 / SpreadsheetML 2003 / Numbers不支持XLSB/XLS/Lotus 1-2-3/SpreadsheetML 2003/Numbers
  • node stream utils removed已删除节点流utils
How to integrate the mini build如何集成迷你构建 (click to show)

Replace references to xlsx.full.min.js with xlsx.mini.min.js. xlsx.mini.min.js替换对xlsx.full.min.js的引用。Starting from scratch, a single script tag should be added at the top of the HTML page:从头开始,应在HTML页面顶部添加一个script标记:

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

Internet Explorer and Older BrowsersInternet Explorer和旧浏览器

For broad compatibility with JavaScript engines, the library is written using ECMAScript 3 language dialect. 为了与JavaScript引擎广泛兼容,该库使用ECMAScript 3语言方言编写。A "shim" script provides implementations of functions for older browsers and environments.“垫片”脚本为较旧的浏览器和环境提供了功能实现。

Due to SSL compatibility issues, older versions of IE will not be able to use the CDN scripts directly. 由于SSL兼容性问题,旧版本的IE将无法直接使用CDN脚本。They should be downloaded and saved to a public directory in the site:应将其下载并保存到网站的公共目录中:

Add a script reference to the shim before the script tag that loads xlsx.js:在加载xlsx.js的脚本标记之前,向填充程序添加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 Workers

The standalone scripts can be loaded using importScripts at the top of the worker scripts:可以使用辅助脚本顶部的importScripts加载独立脚本:

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

ECMAScript Module Imports in a SCRIPT TAGECMAScript模块在脚本标记中导入

caution

This section refers to imports using script type="module". 本节介绍使用script type="module"的导入。For imports in modern projects using Webpack or React or Angular or Vue, the installation is described in the next section.对于使用Webpack或React或Angular或Vue的现代项目中的导入,安装将在下一节中介绍。

The ECMAScript Module build is saved to xlsx.mjs and can be directly added to a page with a script tag using type="module":ECMAScript模块构建保存到xlsx.mjs,可以使用type="module"直接添加到带有脚本script的页面:

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

If XLS support is required, cpexcel.full.mjs must be manually imported:如果需要XLS支持,则必须手动导入cpexcel.full.mjs

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

Dynamic imports with import() can be used in data export scenarios. 可以在数据导出场景中使用带有import()的动态导入。This example will download the library only when the export button is pressed:此示例仅在按下导出按钮时下载库:

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

/* dynamically import the library in the event listener */
const XLSX = await import("https://cdn.sheetjs.com/xlsx-0.18.9/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>

Bower

caution

Bower is deprecated and the maintainers recommend using other tools.Bower已被弃用,维护人员建议使用其他工具。

Bower plays nice with the CDN tarballs:很好地使用CDN tarballs:

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

Bower will place the standalone scripts in bower_components/js-xlsx/dist/Bower将把独立脚本放在bower_components/js-xlsx/dist/中。