Skip to main content

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参数是必需的。它必须指定

  • bookType (file format of the exported file)(导出文件的文件格式)
  • type (return value type)(返回值类型)

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.如果省略了optionsoptions对象中缺少bookType,则输出文件格式将从文件扩展名推导出来。

XLSX.writeXLSX(wb, options)

XLSX.writeFileXLSX(wb, filename, options)

writeXLSX and writeFileXLSX are limited versions of write and writeFile. writeXLSXwriteFileXLSXwritewriteFile的有限版本。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 write and writeFile functions are more appropriate when exporting to XLS or XLSB or other formats.当导出为XLS或XLSB或其他格式时,一般的writewriteFile函数更合适。

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 fs.writeFile under the hood.此方法仅适用于NodeJS,并在后台使用fs.writeFile

Recommendation

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参数:

Option Name选项名称DefaultDescription描述
typeOutput data encoding (see Output Type below)输出数据编码(请参阅下面的输出类型)
cellDatesfalseStore dates as type d (default is n)将日期存储为类型d(默认为n
cellStylesfalseSave style/theme info to the .s field将样式/主题信息保存到.s字段
codepageIf specified, use code page when appropriate如果指定,在适当的时候使用代码页 **
bookSSTfalseGenerate Shared String Table生成共享字符串表 **
bookType"xlsx"Type of Workbook (see below for supported formats)工作簿类型(有关支持的格式,请参阅下文)
bookVBAAdd VBA blob from workbook object to the file将工作簿对象中的VBA blob添加到文件中 **
WTFfalseIf true, throw errors on unexpected features如果为true,则在意外功能上引发错误 **
sheet""Name of Worksheet for single-sheet formats单页格式的工作表名称 **
compressionfalseUse ZIP compression for ZIP-based formats对基于ZIP的格式使用ZIP压缩 **
PropsOverride workbook properties when writing写入时覆盖工作簿属性 **
themeXLSXOverride theme XML when writing XLSX/XLSB/XLSM编写XLSX/XLSB/XLSM时覆盖主题XML **
ignoreECtrueSuppress "number as text" errors抑制“数字为文本”错误 **
numbersPayload for NUMBERS exportNUMBERS导出的有效负载 **
FS",""Field Separator" delimiter between fields字段之间的“字段分隔符”分隔符 **
RS"\n""Record Separator" delimiter between rows行之间的“记录分隔符”分隔符 **
  • 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 type d so non-Excel tools may ignore the data or error in the presence of dates.Excel本身通常不编写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 (to xl/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 to false to suppress.ignoreEC设置为false以进行抑制。
  • FS and RS apply to CSV and Text output formats. FSRS适用于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 script加载xlsx.zahl脚本

2) Pass the payload into the numbers option to write or writeFile.将有效负载传递到numbers选项中以writewriteFile

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>

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选项控制:

bookTypeextension扩大sheetsDescription描述
xlsx.xlsxmultiExcel 2007+ XML Format
xlsm.xlsmmultiExcel 2007+ Macro XML Format
xlsb.xlsbmultiExcel 2007+ Binary Format
biff8.xlsmultiExcel 97-2004 Workbook Format
biff5.xlsmultiExcel 5.0/95 Workbook Format
biff4.xlssingleExcel 4.0 Worksheet Format
biff3.xlssingleExcel 3.0 Worksheet Format
biff2.xlssingleExcel 2.0 Worksheet Format
xlml.xlsmultiExcel 2003-2004 (SpreadsheetML)
numbers.numbersmultiNumbers 3.0+ Spreadsheet
ods.odsmultiOpenDocument Spreadsheet
fods.fodsmultiFlat OpenDocument Spreadsheet
wk3.wk3multiLotus Workbook (WK3)
csv.csvsingleComma Separated Values
txt.txtsingleUTF-16 Unicode Text (TXT)
sylk.sylksingleSymbolic Link (SYLK)
html.htmlsingleHTML Document
dif.difsingleData Interchange Format (DIF)
dbf.dbfsingledBASE II + VFP Extensions (DBF)
wk1.wk1singleLotus Worksheet (WK1)
rtf.rtfsingleRich Text Format (RTF)
prn.prnsingleLotus Formatted Text
eth.ethsingleEthercalc 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形式:

typeoutput输出
"base64"string: Base64 encoding of the filestring:文件的Base64编码
"binary"string: binary string (byte n is data.charCodeAt(n))string:二进制字符串(字节ndata.charCodeAt(n)
"string"string: JS string (not compatible with binary formats)string:JS字符串(与二进制格式不兼容)
"buffer"nodejs Buffer
"array"ArrayBuffer, fallback array of 8-bit unsigned intArrayBuffer,8位无符号int的回退数组
"file"string: path of file that will be created (nodejs only)string:将要创建的文件的路径(仅限nodejs)
  • For compatibility with Excel, csv output will always include the UTF-8 byte order mark.为了与Excel兼容,csv输出将始终包含UTF-8字节顺序标记。