JavaScript API
JavaScript API
phpxlsx Premium licenses include jsdocumentsxlsx, a JavaScript library to work with XLSX spreadsheets using nodejs, web browsers, and other JavaScript projects.
Developed with TypeScript, jsdocumentsxlsx is the best and most complete JavaScript library to generate XLSX spreadsheets from scratch and using XLSX templates.
Among its many features, the following can be highlighted:
- Add new contents.
- Apply styles.
- Replace contents in templates.
- Modify layouts.
- Set settings.
- RTL settings.
- Transform measuring units.
- ...
To achieve the best possible compatibility with nodejs and web browsers, jsdocumentsxlsx uses TypeScript with the following compiler options:
The following formats are included in the package:
- CommonJS: cjs
- ESM: mjs
- IIFE: iife.js
in addition to map files and TypeScript type information.
To use jsdocumentsxlsx it's not mandatory to develop with TypeScript, but it's recommended. Vanilla JavaScript can also be used with jsdocumentsxlsx.
Creating a new XLSX is as simple as instance the CreateXlsx class, add new contents using the available methods and save the document.
The save method returns a Promise object.
For example, to add a cell using TypeScript with nodejs:
fs.writeFileSync is available in nodejs File system.
The same sample using Vanilla JavaScript with nodejs and the ESM module:
The same sample using Vanilla JavaScript with nodejs and CommonJS:
The following sample (generateXlsx.js) generates and downloads a new XLSX in a web browser with the ESM module:
saveAs, available in FileSaver.js is the easiest way to download the document in a web browser.
The same sample using the IIFE format:
The openTemplate method is used to open a XLSX template and replace, remove and add new contents.
The openTemplate method returns a Promise object.
For example, to open a XLSX template and replace text variables in the document target using TypeScript with nodejs:
fs.readFileSync is available in nodejs File system.
The same sample using Vanilla JavaScript with nodejs and the ESM module:
The following sample (generateXlsxFromTemplate.js) opens an XLSX template, generates and downloads a new XLSX in a web browser:
Using FileReader API is the easiest approach to generate an ArrayBuffer from a local file (a File or a Blob object) in a web browser.
In addition to replacing contents, jsdocumentsxlsx includes methods to delete contents, get existing variables, and customise the symbols used to define variables.
External files such as images, XLSX templates and XLSX outputs in jsdocumentsxlsx use ArrayBuffer objects for maximum compatibility. Any JavaScript function or library compatible with ArrayBuffer can be used.
For example, to load an image using TypeScript with nodejs:
The same sample using Vanilla JavaScript with nodejs and the ESM module:
Using the FileReader API is the easiest approach to generate an ArrayBuffer from a local file (a File or a Blob object) in a web browser:
The Fetch API can be used to fetch external files.
On JavaScript API are available the documentation pages, with all methods, options and samples of use. In addition to this documentation, phpxlsx Premium packages include many samples to illustrate how to use all methods.
jsdocumentsxlsx has an official website at jsdocuments.com.