Writing Files写入文件
XLSX.write(wb, options)
write
attempts to write the workbook 尝试写入工作簿wb
and return the file.wb
并返回文件。
The options
argument is required. It must specifyoptions
参数是必需的。它必须指定
XLSX.writeFile(wb, filename, options)
writeFile
attempts to write 尝试将wb
to a local file with specified filename
.wb
写入具有指定filename
的本地文件。
In browser-based environments, it will attempt to force a client-side download. 在基于浏览器的环境中,它将尝试强制客户端下载。It also supports NodeJS, ExtendScript applications, and Chromium extensions.它还支持NodeJS、ExtendeScript应用程序和Chromium扩展。
If 如果省略了options
is omitted or if bookType
is missing from the options
object, the output file format will be deduced from the filename extension.options
或options
对象中缺少bookType
,则输出文件格式将从文件扩展名推导出来。
XLSX.writeXLSX(wb, options)
XLSX.writeFileXLSX(wb, filename, options)
writeXLSX
and writeFileXLSX
are limited versions of write
and writeFile
. writeXLSX
和writeFileXLSX
是write
和writeFile
的有限版本。They support writing to the XLSX file format.它们支持写入XLSX文件格式。
For websites that exclusively export to XLSX, these functions can reduce the size of the production site. 对于专门导出到XLSX的网站,这些功能可以缩小生产网站的大小。The general 当导出为XLS或XLSB或其他格式时,一般的write
and writeFile
functions are more appropriate when exporting to XLS or XLSB or other formats.write
和writeFile
函数更合适。
NodeJS-specific methods (click to show)
XLSX.writeFileAsync(filename, wb, cb)
XLSX.writeFileAsync(filename, wb, options, cb)
attempt to write 尝试将wb
to filename
and invoke the callback cb
on completion.wb
写入filename
,并在完成时调用回调cb
。
When an 当指定了一个options
object is specified, it is expected to be the third argument.options
对象时,它应该是第三个参数。
This method only works in NodeJS and uses 此方法仅适用于NodeJS,并在后台使用fs.writeFile
under the hood.fs.writeFile
。
writeFile
wraps a number of export techniques, making it suitable for browser downloads, NodeJS, ExtendScript apps, and Chromium extensions. 封装了许多导出技术,使其适用于浏览器下载、NodeJS、ExtendScript应用程序和Chromium扩展。It does not work in other environments with more advanced export methods.它不适用于具有更高级导出方法的其他环境。
The write
method returns raw bytes or strings that can be exported in Desktop apps , Mobile apps , and Servers.write
方法返回可以在桌面应用程序、移动应用程序和服务器中导出的原始字节或字符串。
The demos preferentially use 演示文档优先使用writeFile
. When writeFile
is not supported, the demos show file creation using write
and platform APIs.writeFile
。当不支持writeFile
时,演示文档显示使用write
和平台API创建文件。
Writing Options写入选项
The write functions accept an options argument:write
函数接受一个options
参数:
Default | ||
---|---|---|
type | ||
cellDates | false | d (default is n )d (默认为n ) |
cellStyles | false | .s field.s 字段 |
codepage | ||
bookSST | false | |
bookType | "xlsx" | |
bookVBA | ||
WTF | false | true ,则在意外功能上引发错误 ** |
sheet | "" | |
compression | false | |
Props | ||
themeXLSX | ||
ignoreEC | true | |
numbers | ||
FS | "," | |
RS | "\n" |
bookSST
is slower and more memory intensive, but has better compatibility with older versions of iOS Numbers速度较慢,内存更密集,但与旧版本的iOS Numbers具有更好的兼容性The raw data is the only thing guaranteed to be saved. Features not described in this README may not be serialized.原始数据是唯一可以保证保存的东西。本自述文件中未描述的功能可能无法序列化。cellDates
only applies to XLSX output and is not guaranteed to work with third-party readers.仅适用于XLSX输出,不能保证与第三方读取器一起使用。Excel itself does not usually write cells with typeExcel本身通常不编写d
so non-Excel tools may ignore the data or error in the presence of dates.d
类型的单元格,因此非Excel工具可能会在存在日期的情况下忽略数据或错误。codepage
is applied to legacy formats including DBF.应用于包括DBF在内的遗留格式。Characters missing from the encoding will be replaced with underscore characters (编码中缺少的字符将替换为下划线字符(_
)._
)。Props
is an object mirroring the workbook是一个镜像工作簿Props
field.Props
字段的对象。See the table from the Workbook File Properties section.请参阅工作簿文件属性部分的表格。if specified, the string from如果指定,themeXLSX
will be saved as the primary theme for XLSX/XLSB/XLSM files (toxl/theme/theme1.xml
in the ZIP)themeXLSX
中的字符串将保存为XLSX/XLSB/XLSM文件的主主题(到ZIP中的xl/theme/theme1.xml
)Due to a bug in the program, some features like "Text to Columns" will crash Excel on worksheets where error conditions are ignored.由于程序中的一个错误,一些功能,如“文本到列”,将使Excel在忽略错误条件的工作表上崩溃。The writer will mark files to ignore the error by default.默认情况下,编写器会将文件标记为忽略该错误。Set将ignoreEC
tofalse
to suppress.ignoreEC
设置为false
以进行抑制。FS
andRS
apply to CSV and Text output formats.FS
和RS
适用于CSV和Text输出格式。The options are discussed in "CSV and Text"选项在“CSV和文本”中进行了讨论bookVBA
only applies to supported formats.仅适用于支持的格式。"VBA"section explains the feature in more detail.部分更详细地解释了该功能。WTF
is mainly for development.主要用于发展。
Exporting NUMBERS files (click to show)
The NUMBERS writer requires a fairly large base. NUMBERS编写器需要一个相当大的基础。The supplementary 补充的xlsx.zahl
scripts provide support. xlsx.zahl
脚本提供了支持。 xlsx.zahl.js
is designed for standalone and NodeJS use, while xlsx.zahl.mjs
is suitable for ESM.xlsx.zahl.js
是为独立和NodeJS使用而设计的,而xlsx.zahl.mjs
适用于ESM。
Adding NUMBERS export support involves two steps:添加NUMBERS导出支持包括两个步骤:
1) Load the 加载xlsx.zahl
scriptxlsx.zahl
脚本
2) Pass the payload into the 将有效负载传递到numbers
option to write
or writeFile
.numbers
选项中以write
或writeFile
。
- Browser
- NodeJS
- Bun
- Deno
https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.zahl.js is the URL for 0.20.1
<meta charset="utf8">
<body>
<script src="https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js"></script>
<script src="https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.zahl.js"></script>
<script>
var wb = XLSX.utils.book_new(); var ws = XLSX.utils.aoa_to_sheet([
["SheetJS", "<3","விரிதாள்"],
[72,,"Arbeitsblätter"],
[,62,"数据"],
[true,false,],
]); XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "textport.numbers", {numbers: XLSX_ZAHL_PAYLOAD, compression: true});
</script>
</body>
After installing the package:安装软件包后:
npm i --save https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz
The scripts will be available at 脚本将在xlsx/dist/xlsx.zahl
(CommonJS) and xlsx/dist/xlsx.zahl.mjs
(ESM).xlsx/dist/xlsx.zahl
(CommonJS)和xlsx/dist/xlsx.zahl.mjs
(ESM)中提供。
var XLSX = require("xlsx");
var XLSX_ZAHL_PAYLOAD = require("xlsx/dist/xlsx.zahl");
var wb = XLSX.utils.book_new(); var ws = XLSX.utils.aoa_to_sheet([
["SheetJS", "<3","விரிதாள்"],
[72,,"Arbeitsblätter"],
[,62,"数据"],
[true,false,],
]); XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "textport.numbers", {numbers: XLSX_ZAHL_PAYLOAD, compression: true});
After installing the package:安装软件包后:
bun i https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz
The scripts will be available at 脚本将在xlsx/dist/xlsx.zahl
(CommonJS) and xlsx/dist/xlsx.zahl.mjs
(ESM).xlsx/dist/xlsx.zahl
(CommonJS)和xlsx/dist/xlsx.zahl.mjs
(ESM)中提供。
import * as XLSX from "xlsx";
import XLSX_ZAHL_PAYLOAD from "xlsx/dist/xlsx.zahl";
import * as fs from "fs";
XLSX.set_fs(fs);
var wb = XLSX.utils.book_new(); var ws = XLSX.utils.aoa_to_sheet([
["SheetJS", "<3","விரிதாள்"],
[72,,"Arbeitsblätter"],
[,62,"数据"],
[true,false,],
]); XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "textport.numbers", {numbers: XLSX_ZAHL_PAYLOAD, compression: true});
https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.zahl.mjs is the URL for 0.20.1
import * as XLSX from 'https://cdn.sheetjs.com/xlsx-0.20.1/package/xlsx.mjs';
import XLSX_ZAHL_PAYLOAD from 'https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.zahl.mjs';
var wb = XLSX.utils.book_new(); var ws = XLSX.utils.aoa_to_sheet([
["SheetJS", "<3","விரிதாள்"],
[72,,"Arbeitsblätter"],
[,62,"数据"],
[true,false,],
]); XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "textport.numbers", {numbers: XLSX_ZAHL_PAYLOAD, compression: true});
Supported Output Formats支持的输出格式
For broad compatibility with third-party tools, this library supports many output formats. The specific file type is controlled with 为了与第三方工具广泛兼容,此库支持许多输出格式。特定的文件类型由bookType
option:bookType
选项控制:
bookType | sheets | ||
---|---|---|---|
xlsx | .xlsx | multi | Excel 2007+ XML Format |
xlsm | .xlsm | multi | Excel 2007+ Macro XML Format |
xlsb | .xlsb | multi | Excel 2007+ Binary Format |
biff8 | .xls | multi | Excel 97-2004 Workbook Format |
biff5 | .xls | multi | Excel 5.0/95 Workbook Format |
biff4 | .xls | single | Excel 4.0 Worksheet Format |
biff3 | .xls | single | Excel 3.0 Worksheet Format |
biff2 | .xls | single | Excel 2.0 Worksheet Format |
xlml | .xls | multi | Excel 2003-2004 (SpreadsheetML) |
numbers | .numbers | multi | Numbers 3.0+ Spreadsheet |
ods | .ods | multi | OpenDocument Spreadsheet |
fods | .fods | multi | Flat OpenDocument Spreadsheet |
wk3 | .wk3 | multi | Lotus Workbook (WK3) |
csv | .csv | single | Comma Separated Values |
txt | .txt | single | UTF-16 Unicode Text (TXT) |
sylk | .sylk | single | Symbolic Link (SYLK) |
html | .html | single | HTML Document |
dif | .dif | single | Data Interchange Format (DIF) |
dbf | .dbf | single | dBASE II + VFP Extensions (DBF) |
wk1 | .wk1 | single | Lotus Worksheet (WK1) |
rtf | .rtf | single | Rich Text Format (RTF) |
prn | .prn | single | Lotus Formatted Text |
eth | .eth | single | Ethercalc Record Format (ETH) |
compression
applies to ZIP-based formats (XLSX, XLSM, XLSB, NUMBERS, ODS)适用于基于ZIP的格式(XLSX、XLSM、XLSB、NUMBERS、ODS)Formats that only support a single sheet require a仅支持单个工作表的格式需要指定工作表的sheet
option specifying the worksheet. If the string is empty, the first worksheet is used.sheet
选项。如果字符串为空,则使用第一个工作表。writeFile
will automatically guess the output file format based on the file extension if如果未指定bookType
is not specified.bookType
,将根据文件扩展名自动猜测输出文件格式。It will choose the first format in the aforementioned table that matches the extension.它将选择上述表中与扩展名匹配的第一种格式。
Output Type输出类型
The type
option specifies the JS form of the output:type
选项指定输出的JS形式:
type | |
---|---|
"base64" | |
"binary" | n is data.charCodeAt(n) )n 为data.charCodeAt(n) ) |
"string" | |
"buffer" | nodejs Buffer |
"array" | |
"file" |
For compatibility with Excel,为了与Excel兼容,csv
output will always include the UTF-8 byte order mark.csv
输出将始终包含UTF-8字节顺序标记。