Standalone Browser Scripts独立浏览器脚本
Each standalone release script is available at https://cdn.sheetjs.com/.每个独立的发布脚本都可以在https://cdn.sheetjs.com/下载到。
The current version is 0.20.1 and can be referenced as follows:当前版本为0.20.1,可参考如下:
<!-- use version 0.20.1 -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js"></script>
Watch the repo or subscribe to the RSS feed to be notified when new versions are released!观看Repo或订阅RSS订阅源,以便在发布新版本时收到通知!
A number of services host older versions of the SheetJS libraries. 许多服务托管较旧版本的SheetJS库。 Due to syncing issues, they are generally out of date.由于同步问题,它们通常已过时。
The SheetJS CDN https://cdn.sheetjs.com/ is the authoritative source for SheetJS scriptsSheetJS CDN https://cdn.sheetjs.com/是SheetJS脚本的权威来源
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.20.1 -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js"></script>
A slimmer build is generated at 在dist/xlsx.mini.min.js
. Compared to full build:dist/xlsx.mini.min.js
中生成了一个更精简的构建。与完整构建相比:
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/Numbersnode stream utils removed已删除节点流实用程序
How to integrate the mini build如何集成小型构建 (click to show)
Replace references to 将对xlsx.full.min.js
with xlsx.mini.min.js
. xlsx.full.min.js
的引用替换为xlsx.mini.min.js
。Starting from scratch, a single script tag should be added at the top of the HTML page:从头开始,应该在HTML页面的顶部添加一个脚本标记:
<!-- use xlsx.mini.min.js from version 0.20.1 -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.mini.min.js"></script>
Vendoring
For general stability, "vendoring" scripts is the recommended approach:为了获得总体稳定性,建议使用“vendoring”脚本:
1) Download the script (下载所需版本的脚本(xlsx.full.min.js
) for the desired version. xlsx.full.min.js
)。The current version is available at https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js当前版本可在https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js下载到。
2) Move the script to a 将脚本与其他脚本一起移动到public
folder with other scripts.public
文件夹中。
3) Reference the local script from HTML pages:从HTML页面引用本地脚本:
<script src="/public/xlsx.full.min.js"></script>
This script assigns to 此脚本指定给window.XLSX
. window.XLSX
。The global can be used in other scripts.全局可以在其他脚本中使用。
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:它们应该被下载并保存到网站的公共目录中:
- Standalone: https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.mini.min.js
- Shim: https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/shim.min.js
Add a 在独立脚本之前添加对填充程序的script
reference to the shim before the standalone script: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 可以在workder脚本顶部使用importScripts
at the top of the worker scripts:importScripts
加载独立脚本:
importScripts("https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/shim.min.js");
importScripts("https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js");
ECMAScript Module Imports模块导入
This section refers to imports using 本节涉及使用script type="module"
. script type="module"
的导入。For imports in modern projects using Webpack or React or Angular or VueJS, the installation is described in "Frameworks and Bundlers".对于使用Webpack、React、Angular或VueJS的现代项目中的导入,安装在“Frameworks and Bundlers”中进行了描述。
The ECMAScript Module build is saved to ECMAScript模块构建保存在xlsx.mjs
and can be directly added to a page with a script
tag using type="module"
:xlsx.mjs
中,可以使用type="module"
直接添加到带有script
标记的页面中:
<script type="module">
import { read, writeFileXLSX } from "https://cdn.sheetjs.com/xlsx-0.20.1/package/xlsx.mjs";
</script>
If Encoding support is required, 如果需要编码支持,则必须手动导入cpexcel.full.mjs
must be manually imported: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.20.1/package/xlsx.mjs";
import * as cptable from 'https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/cpexcel.full.mjs';
set_cptable(cptable);
</script>
Web Worker support is noted in the "Web Workers" demo“Web Workers”演示中提到了对Web Worker的支持
Dynamic Imports动态导入
Dynamic imports with 使用import()
will only download the SheetJS scripts when they are used. This example will download the library when data is exported:import()
的动态导入只会在使用SheetJS脚本时下载它们。此示例将在导出数据时下载库:
<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.1/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>
The callback functions must be marked as 回调函数必须标记为async
and the script block must have the attribute type="module"
async
,并且脚本块必须具有属性type="module"
If Encoding support is required, 如果需要编码支持,则必须手动导入cpexcel.full.mjs
must be manually imported:cpexcel.full.mjs
:
<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.1/package/xlsx.mjs");
const cptable = await import("https://cdn.sheetjs.com/xlsx-0.20.1/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 is deprecated and the maintainers recommend using other tools.Bower已被弃用,维护人员建议使用其他工具。
The Bower package manager plays nice with the CDN tarballs:Bower软件包管理器在CDN tarball方面表现出色:
npx bower install https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz
Bower will place the standalone scripts in Bower将把独立脚本放在bower_components/js-xlsx/dist/
bower_components/js-xlsx/dist/
中