Create XLSX Documents with ExcelJS
You can create fully customizable Excel (XLSX) documents inside your MPage with the help of excelJS and file-saver. Both of these libraries are completely free to use in your hospital and commercial MPage products.
List of Third Party libraries used in this tutorial
- excelJS offers the functionality needed to create an XLSX spreadsheet in memory.
- file-saver creates a downloadable file from a blob in memory.
- @types/file-saver contains the TypeScript types needed to use file-saver in your Angular application.
Install the third party libraries
npm install exceljs npm install file-saver npm install --save-dev @types/file-saver
Imports
You must import the Workbook and saveAs classes inside the class where you are generating your document.
import { Workbook } from 'exceljs';
import { saveAs } from 'file-saver';
Generate and deliver the XLSX document
The code to generate your XLSX document must be contained in an async function and return a Promise. For example:
protected async createXLXS(): Promise<void> {
...your code...
}
Inside your function, you will need to perform the following tasks:
- Create a new Workbook
- Set any desired properties
- Add one or more Worksheets
- Populate the Worksheets with content
- Write the Workbook to a Buffer object
- Convert the Buffer object to a Blob
- Use file-saver to save your file
This tutorial will create a basic spreadsheet that you can experiment with. Full documentation for excelJS can be found at https://github.com/exceljs/exceljs#readme. The functionality exposed in excelJS is too extensive to include in this tutorial.
protected async createXLXS(): Promise<void> {
...all further code goes here!...
}
To create a Workbook, simply assign a new object as follows.
const workbook: Workbook = new Workbook();
To optionally set properties for your Workbook, do so by assigning your desired values.
workbook.creator = 'Precision Healthcare Solutions'; workbook.lastModifiedBy = 'Precision Healthcare Solutions'; workbook.created = new Date(); workbook.title = 'Test document';
Create a Worksheet inside your workbook. The first parameter is the name of the worksheet. There are optional additional parameters to perform tasks such as changing the worksheet tab color that can be found in the official excelJS documentation.
const worksheet = workbook.addWorksheet('Test 123');
You can control the appearance of each column by defining columns on your worksheet. In the example below, we are adding a "header" property that creates a title row in the spreadsheet. The "key" value is used for assigning data to the proper column. "width" and "style" properties alter the appearance of the cell.
worksheet.columns = [
{header: 'MRN', key: 'mrn', width: 20, style: {alignment: {horizontal: 'center'}}},
{header: 'Name', key: 'name', width: 32},
{header: 'Age', key: 'age', width: 10},
]
Adding data can be done at the cell level (see official documentation) or added by row as we will demonstrate here. A row is added with the addRow method and can either be an object using the key values assigned in the column definition or by simply passing an array in order with the desired values.
worksheet.addRow({mrn: '123456', name: 'Fake Patient', age: 30});
worksheet.addRow({mrn: '395854', name: 'Test Patient', age: 25});
worksheet.addRow(['334433','Test Patient 2', 56]);
At this point our spreadsheet is ready for output. There are many more features than shown here including column freezing, shading/colors, multiple tabs, calculations and more.
Our final step is to convert our in-memory spreadsheet into a downloadable file. This is first done by converting our workbook into a Buffer, which is then passed to a Blob object. This Blob is then saved using the file-saver library.
const buffer = await workbook.xlsx.writeBuffer();
const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; // Set the output file format
const blob = new Blob([buffer], {type: fileType});
saveAs(blob, 'myFileName.xlsx'); // Save the output to a file
Triggering the spreadsheet creation
Now that our spreadsheet function is complete, the next step is to execute it from either a <button> click or in your code somewhere (e.g. custom component, service, etc.).
<button (click)="createXLXS()">Create Spreadsheet</button>
