Skip to main content

Writing Files

The main SheetJS method for writing workbooks is write. Scripts receive common JavaScript data representations and are expected to write or share files using platform-specific APIs.

The writeFile helper method accepts a filename and tries to write to a local file using standard APIs.

Export a SheetJS workbook object in a specified file format

var file_data = XLSX.write(wb, opts);

write attempts to write the workbook wb and return the file.

The options argument is required. It must specify

  • bookType (file format of the exported file)
  • type (return value type)

Export a SheetJS workbook object and attempt to write a local file

XLSX.writeFile(wb, filename, options);

writeFile attempts to write wb to a local file with specified filename.

In browser-based environments, it will attempt to force a client-side download. It also supports NodeJS, ExtendScript applications, and Chromium extensions.

If options is omitted or if bookType is missing from the options object, the output file format will be deduced from the filename extension.

Special functions for exporting data in the XLSX format

// limited form of `write`
var file_data = XLSX.writeXLSX(wb, options);


// limited form of `writeFile`
XLSX.writeFileXLSX(wb, filename, options);

writeXLSX and writeFileXLSX are limited versions of write and writeFile. They support writing to the XLSX file format.

For websites that exclusively export to XLSX, these functions can reduce the size of the production site. The general write and writeFile functions are more appropriate when exporting to XLS or XLSB or other formats.

NodeJS-specific methods (click to show)

Export a workbook and attempt to write a local file using fs.writeFile

// callback equivalent of `XLSX.writeFile`
XLSX.writeFileAsync(filename, wb, cb);

// callback equivalent with options argument
XLSX.writeFileAsync(filename, wb, options, cb);

writeFileAsync attempts to write wb to filename and invoke the callback cb on completion.

When an options object is specified, it is expected to be the third argument.

This method only works in NodeJS and uses fs.writeFile under the hood.

Recommendation

writeFile wraps a number of export techniques, making it suitable for browser downloads, NodeJS, ExtendScript apps, and Chromium extensions. 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.

The demos preferentially use writeFile. When writeFile is not supported, the demos show file creation using write and platform APIs.

Writing Options

The write functions accept an options argument:

Option NameDefaultDescription
typeOutput data encoding (see Output Type below)
cellDatesfalseStore dates as type d (default is n)
cellStylesfalseSave style/theme info to the .s field
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 **
WTFfalseIf true, throw errors on unexpected features **
sheet""Name of Worksheet for single-sheet formats **
compressionfalseUse ZIP compression for ZIP-based formats **
PropsOverride workbook properties when writing **
themeXLSXOverride theme XML when writing XLSX/XLSB/XLSM **
ignoreECtrueSuppress "number as text" errors **
numbersPayload for NUMBERS export **
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
  • 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. 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.
  • codepage is applied to legacy formats including DBF. Characters missing from the encoding will be replaced with underscore characters (_).
  • Props is an object mirroring the workbook Props field. 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)
  • Due to a bug in the program, some features like "Text to Columns" will crash Excel on worksheets where error conditions are ignored. The writer will mark files to ignore the error by default. Set ignoreEC to false to suppress.
  • FS and RS apply to CSV and Text output formats. The options are discussed in "CSV and Text"
  • 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. The supplementary xlsx.zahl scripts provide support. xlsx.zahl.js is designed for standalone and NodeJS use, while xlsx.zahl.mjs is suitable for ESM.

Adding NUMBERS export support involves two steps:

  1. Load the xlsx.zahl script

  2. Pass the payload into the numbers option to write or writeFile.

https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.zahl.js is the URL for 0.20.3

<meta charset="utf8">
<body>
<script src="https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js"></script>
<script src="https://cdn.sheetjs.com/xlsx-0.20.3/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:

bookTypeextensionsheetsDescription
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)
  • Formats that only support a single sheet require a sheet option specifying the worksheet. If the string is empty, the first worksheet is used.
  • writeFile will automatically guess the output file format based on the file extension if bookType is not specified. 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:

typeoutput
"base64"string: Base64 encoding of the file
"binary"string: binary string (byte n is data.charCodeAt(n))
"string"string: JS string (not compatible with binary formats)
"buffer"nodejs Buffer
"array"ArrayBuffer, fallback array of 8-bit unsigned int
"file"string: path of file that will be created (nodejs only)

For compatibility with Excel, csv output will always include the UTF-8 byte order mark ("BOM").

The raw sheet_to_csv method will return JavaScript strings without the UTF-8 BOM.