Skip to main content

NodeJS

Tarballs are available on https://cdn.sheetjs.com.Tarballs可在https://cdn.sheetjs.com上获得。

Each individual version can be referenced using a similar URL pattern.可以使用类似的URL模式引用每个单独的版本。
https://cdn.sheetjs.com/xlsx-0.18.9/xlsx-0.18.9.tgz is the URL for 0.18.9是0.18.9的URL

Installation安装

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

$ yarn add --save https://cdn.sheetjs.com/xlsx-0.18.9/xlsx-0.18.9.tgz

Vendoring供应商

For general stability, "vendoring" modules is the recommended approach:对于一般稳定性,“供应商”模块是推荐的方法:

1) Download the tarball (xlsx-0.18.9.tgz) for the desired version. 下载所需版本的tarball(xlsx-0.18.9.tgz)。The current version is available at https://cdn.sheetjs.com/xlsx-0.18.9/xlsx-0.18.9.tgz当前版本可从以下网址获得:https://cdn.sheetjs.com/xlsx-0.18.9/xlsx-0.18.9.tgz

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

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

$ yarn add --save file:vendor/xlsx-0.18.9.tgz

The package will be installed and accessible as xlsx.该软件包将作为xlsx安装和访问。

Usage用法

CommonJS require

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

var XLSX = require("xlsx");

ESM import

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

import * as XLSX from 'xlsx/xlsx.mjs';

/* load 'fs' for readFile and writeFile support */
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);