Skip to main content

Frameworks and Bundlers框架和捆绑器

Each standalone release package is available at https://cdn.sheetjs.com/. 每个独立的发布包都可以在https://cdn.sheetjs.com/下载到。 The NodeJS package is designed to be used with frameworks and bundlers. NodeJS包设计用于框架和捆绑器。It is a proper ECMAScript Module release which can be optimized with developer tools.这是一个合适的ECMAScript模块版本,可以使用开发人员工具进行优化。

https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz is the URL for version 0.20.1是版本0.20.1的URL

Installation安装

Tarballs can be directly installed using a package manager:Tarball可以使用软件包管理器直接安装:

yarn add https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz

Once installed, the library can be imported under the name xlsx:安装后,可以以名称xlsx导入库:

import { read, writeFileXLSX } from "xlsx";

The "Bundlers" demo includes examples for specific tools.“Bundlers”演示包括特定工具的示例。

Watch the repo or subscribe to the RSS feed to be notified when new versions are released!观看Repo或订阅RSS订阅源,以便在发布新版本时收到通知!

Snyk Bugs

Snyk security tooling may report errors involving "Prototype Pollution":Snyk安全工具可能会报告涉及“原型污染”的错误:

Prototype Pollution [Medium Severity][https://security.snyk.io/vuln/SNYK-JS-XLSX-5457926]

As noted in the Snyk report:Snyk报告所述:

The issue is resolved in version 0.19.3该问题在0.19.3版本中得到解决

Snyk is falsely reporting vulnerabilities. It is a bug in the Snyk tooling.Snyk谎报漏洞。这是Snyk工具中的一个错误。

Until Snyk fixes the bugs, the official recommendation is to suppress the warning.在Snyk修复错误之前,官方建议取消警告

Legacy Endpoints遗留终结点

Older releases are technically available on the public npm registry as xlsx, but the registry is out of date. The latest version on that registry is 0.18.5从技术上讲,旧版本在公共npm注册表中以xlsx的形式提供,但该注册表已过期。该注册表的最新版本为0.18.5

This is a known registry bug这是一个已知的注册表错误

The SheetJS CDN https://cdn.sheetjs.com/ is the authoritative source for SheetJS modules.SheetJS CDN https://cdn.sheetjs.com/是SheetJS模块的权威来源。

For existing projects, the easiest approach is to uninstall and reinstall:对于现有项目,最简单的方法是卸载并重新安装:

yarn remove xlsx
yarn add https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz

When the xlsx library is a dependency of a dependency, the overrides field in package.json can control module resolution:xlsx库是依赖项的依赖项时,package.json中的overrides字段可以控制模块解析:

package.json
{
"overrides": {
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz"
}
}

Vendoring

For general stability, "vendoring" modules is the recommended approach:对于一般稳定性,建议采用“vendoring”模块:

1) Download the tarball (xlsx-0.20.1.tgz) for the desired version. The current version is available at https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz下载所需版本的tarball(xlsx-0.20.1.tgz)。当前版本可在https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz下载到。

2) Create a vendor subfolder at the root of your project and move the tarball to that folder. Add it to your project repository.在项目的根目录下创建一个vendor子文件夹,然后将tarball移动到该文件夹中。将其添加到项目存储库中。

3) Install the tarball using a package manager:使用软件包管理器安装tarball:

yarn add file:vendor/xlsx-0.20.1.tgz

The package will be installed and accessible as xlsx.该软件包将以xlsx的形式安装并可访问。

Usage用法

With most frameworks and bundler tools, named imports are recommended:对于大多数框架和bundler工具,建议使用命名导入:

import { read, utils } from 'xlsx';

Some legacy bundlers require the glob import:一些遗留捆绑器需要glob导入:

import * as XLSX from 'xlsx';
const { read, utils } = XLSX;

For legacy bundlers that support CommonJS, require will work:对于支持CommonJS的遗留捆绑器,require将起作用:

var XLSX = require("xlsx");
var read = XLSX.read, utils = XLSX.utils;

The "Bundlers" demo includes examples for specific tools.“Bundlers”演示包括特定工具的示例。

Dynamic Imports动态导入

Dynamic imports with import() will only download scripts when they are needed.使用import()的动态导入只会在需要时下载脚本。

Dynamic import will always download the full contents of the imported scripts!动态import将始终下载导入脚本的全部内容!

This is a design flaw in ECMAScript modules这是ECMAScript模块中的一个设计缺陷

It is strongly recommended to use a wrapper script that imports and re-exports the parts of the SheetJS library that are used in a specific function or page:强烈建议使用包装脚本来导入和重新导出特定函数或页面中使用的SheetJS库部分:

SheetJSWriteWrapper.js (wrapper script)
/* This wrapper pulls `writeFileXLSX` and `utils` from the SheetJS library此包装从SheetJS库中提取`writeFileXLSX`和`utils` */
import { utils, writeFileXLSX } from "xlsx";
export { utils, writeFileXLSX };

A dynamic import of the wrapper script will only load the requested features:包装脚本的动态导入将只加载请求的功能:

async function export_data() {
/* dynamically import the SheetJS Wrapper */
const XLSX = await import ("./SheetJSWriteWrapper");
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.writeFileXLSX(wb, "SheetJSDynamicWrapperTest.xlsx");
}

Encoding support编码支持

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

/* load the codepage support library for extended support with older formats加载代码页支持库以获得对旧格式的扩展支持  */
import { set_cptable } from "xlsx";
import * as cptable from 'xlsx/dist/cpexcel.full.mjs';
set_cptable(cptable);