Skip to main content

NodeJS

Package tarballs are available on https://cdn.sheetjs.com.打成包的tarballs可在https://cdn.sheetjs.com下载到。

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

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:

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. 下载所需版本的tarball(xlsx-0.20.1.tgz)。The current version is available at https://cdn.sheetjs.com/xlsx-0.20.1/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用法

The package supports CommonJS require and ESM import module systems.该包支持CommonJSrequire和ESMimport模块系统。

It is strongly recommended to use CommonJS in NodeJS.强烈建议在NodeJS中使用CommonJS。

CommonJS require

By default, the module supports require and it will automatically add support for streams and file system access:默认情况下,该模块支持require,并将自动添加对流和文件系统访问的支持:

var XLSX = require("xlsx");

ESM import

The module also ships with xlsx.mjs for use with import. 该模块还附带了用于导入的xlsx.mjsThe mjs version does not automatically load native node modules, so they must be added manually:mjs版本不会自动加载本机节点模块,因此必须手动添加:

import * as XLSX from 'xlsx';

/* load 'fs' for readFile and writeFile support加载‘fs’以获得readFile和writeFile支持 */
import * as fs from 'fs';
XLSX.set_fs(fs);

/* load 'stream' for stream support为流支持加载“流” */
import { Readable } from 'stream';
XLSX.stream.set_readable(Readable);

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

NextJS

fs cannot be imported from the top level in NextJS pages. This will not work:无法从NextJS页面的顶层导入。这将不起作用:

/* it is safe to import the library from the top level从顶级导入库是安全的 */
import { readFile, utils, set_fs } from 'xlsx';
/* it is not safe to import 'fs' from the top level从顶级导入‘fs’是不安全的 ! */
import * as fs from 'fs'; // this import will fail此导入将失败
set_fs(fs);

fs should be loaded with a dynamic import within a lifecycle function:应在生命周期函数中加载动态导入:

index.js
/* it is safe to import the library from the top level从顶级导入库是安全的 */
import { readFile, utils, set_fs } from 'xlsx';
import { join } from 'path';
import { cwd } from 'process';

export async function getServerSideProps() {
set_fs(await import("fs")); // dynamically import 'fs' in `getServerSideProps`在`getServerSideProps`中动态导入‘fs’
const wb = readFile(join(cwd(), "public", "sheetjs.xlsx"));
// ...
}

The NextJS demo includes complete examples.NextJS演示包括完整的示例。