Data Export
The "Export Tutorial" is a gentle introduction to data processing and export.
Writing Workbooks​
API​
Generate spreadsheet bytes (file) from data
var data = XLSX.write(workbook, opts);
The write
method attempts to package data from the workbook into a file in
memory. By default, XLSX files are generated, but that can be controlled with
the bookType
property of the opts
argument. Based on the type
option,
the data can be stored as a "binary string", JS string, Uint8Array
or Buffer.
The second opts
argument is required. "Writing Options"
covers the supported properties and behaviors.
Generate and attempt to save file
XLSX.writeFile(workbook, filename, opts);
The writeFile
method packages the data and attempts to save the new file. The
export file format is determined by the extension of filename
(SheetJS.xlsx
signals XLSX export, SheetJS.xlsb
signals XLSB export, etc).
The second opts
argument is optional. "Writing Options"
covers the supported properties and behaviors.
Generate and attempt to save an XLSX file
XLSX.writeFileXLSX(workbook, filename, opts);
The writeFile
method embeds a number of different export functions. This is
great for developer experience but not amenable to tree shaking using the
current developer tools. When only XLSX exports are needed, this method avoids
referencing the other export functions.
The second opts
argument is optional. "Writing Options"
covers the supported properties and behaviors.
The writeFile
and writeFileXLSX
methods uses platform-specific APIs to save
files. The APIs do not generally provide feedback on whether files were created.
Examples​
Here are a few common scenarios (click on each subtitle to see the code).
The demos cover special deployments in more detail.
Example: Local File​
XLSX.writeFile
supports writing local files in platforms like NodeJS. In other
platforms like React Native, XLSX.write
should be called with file data.
- Browser
- NodeJS
- Bun
- Deno
- Electron
- React Native
- Photoshop
- Headless
XLSX.writeFile
wraps a few techniques for triggering a file save:
URL
browser API creates an object URL for the file, which the library uses by creating a link and forcing a click. It is supported in modern browsers.msSaveBlob
is an IE10+ API for triggering a file save.IE_FileSave
uses VBScript and ActiveX to write a file in IE6+ for Windows XP and Windows 7. The shim must be included in the containing HTML page.
There is no standard way to determine if the actual file has been downloaded.
/* output format determined by filename */
XLSX.writeFile(workbook, "out.xlsb");
/* at this point, out.xlsb will have been downloaded */
None of the file writing APIs work from Web Workers. To generate a file:
- use
XLSX.write
with typearray
to generate aUint8Array
:
// in the web worker, generate the XLSX file as a Uint8Array
const u8 = XLSX.write(workbook, { type: "array", bookType: "xlsx" });
- send the data back to the main thread:
// in the web worker, send the generated data back to the main thread
postMessage({t: "export", v: u8 });
- from the main thread, add an event listener to write to file:
// in the main page
worker.addEventListener('message', function(e) {
if(e && e.data && e.data.t == "export") {
e.stopPropagation();
e.preventDefault();
// data will be the Uint8Array from the worker
const data = e.data.v;
var blob = new Blob([data], {type:"application/octet-stream"});
var url = URL.createObjectURL(blob);
var a = document.createElement("a");
a.download = "SheetJSXPort.xlsx";
a.href = url;
document.body.appendChild(a);
a.click();
}
});
SWF workaround for Windows 95+ (click to show)
Each moving part in this solution has been deprecated years ago:
- Adobe stopped supporting Flash Player at the end of 2020
- Microsoft stopped supporting IE8 in 2019 and stopped supporting IE9 in 2020
Downloadify
support ended in 2010 andSWFObject
support ended in 2016
New projects should strongly consider requiring modern browsers. This info is provided on an "as is" basis and there is no realistic way to provide support given that every related vendor stopped providing support for their software.
XLSX.writeFile
techniques work for most modern browsers as well as older IE.
For much older browsers, there are workarounds implemented by wrapper libraries.
Downloadify
uses a Flash SWF button
to generate local files, suitable for environments where ActiveX is unavailable:
Downloadify.create(id,{
/* other options are required! read the downloadify docs for more info */
filename: "test.xlsx",
data: function() { return XLSX.write(wb, {bookType:"xlsx", type:"base64"}); },
append: false,
dataType: "base64"
});
The oldie
demo shows an IE-compatible fallback scenario.
writeFile
uses fs.writeFileSync
under the hood:
var XLSX = require("xlsx");
/* output format determined by filename */
XLSX.writeFile(workbook, "out.xlsb");
For Node ESM, fs
must be loaded manually:
import * as fs from "fs";
import { writeFile, set_fs } from "xlsx";
set_fs(fs);
/* output format determined by filename */
writeFile(workbook, "out.xlsb");
As with Node ESM, fs
must be loaded manually:
import * as fs from "fs";
import { writeFile, set_fs } from "xlsx";
set_fs(fs);
/* output format determined by filename */
writeFile(workbook, "out.xlsb");
writeFile
uses Deno.writeFileSync
under the hood:
// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.3/package/types/index.d.ts"
import * as XLSX from 'https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs';
XLSX.writeFile(workbook, "test.xlsx");
Applications writing files must be invoked with the --allow-write
flag.
writeFile
can be used in the renderer process:
/* From the renderer process */
var XLSX = require("xlsx");
XLSX.writeFile(workbook, "out.xlsb");
Electron APIs have changed over time. The electron
demo
shows a complete example and details the required version-specific settings.
The React Native Demo covers tested plugins.
writeFile
wraps the File
logic in Photoshop and other ExtendScript targets.
The specified path should be an absolute path:
#include "xlsx.extendscript.js"
/* Ask user to select path */
var thisFile = File.saveDialog("Select an output file", "*.xlsx;*.xls");
/* output format determined by filename */
XLSX.writeFile(workbook, thisFile.absoluteURI);
The extendscript
demo includes complete
examples for Photoshop and InDesign.
The headless
demo includes complete
examples of converting HTML TABLE elements to XLSB workbooks using Puppeteer
and other headless automation tools.
Headless browsers may not have access to the filesystem, so XLSX.writeFile
may fail. It is strongly recommended to generate the file bytes in the browser
context, send the bytes to the automation context, and write from automation.
Puppeteer and Playwright are NodeJS modules that support binary strings:
/* from the browser context */
var bin = XLSX.write(workbook, { type:"binary", bookType: "xlsb" });
/* from the automation context */
fs.writeFileSync("SheetJSansHead.xlsb", bin, { encoding: "binary" });
PhantomJS fs.write
supports writing files from the main process. The mode
wb
supports binary strings:
/* from the browser context */
var bin = XLSX.write(workbook, { type:"binary", bookType: "xlsb" });
/* from the automation context */
fs.write("SheetJSansHead.xlsb", bin, "wb");