ExcelJS
Read, manipulate and write spreadsheet data and styles to XLSX and JSON.
Reverse engineered from Excel spreadsheet files as a project.
Installation
npm install exceljs
New Features!
-
Streaming XLSX Writer
-
At long last ExcelJS can support writing massive XLSX files in a scalable
memory efficient manner. Performance has been optimised and even smaller spreadsheets
can be faster to write than the document writer. Options have been added to control
the use of shared strings and styles as these can both have a considerable effect on
performance
-
Worksheet.lastRow
- Access the last editable row in a worksheet.
-
Row.commit()
-
For streaming writers, this method commits the row (and any previous rows) to the stream.
Committed rows will no longer be editable (and are typically deleted from the worksheet object).
For Document type workbooks, this method has no effect.
Backlog
- XLSX Streaming Parser
- Parsing CSV with Headers
- Use WeakMap if Available
- Investigate streaming zip
Contents
Interface
var Excel = require("exceljs");
Create a Workbook
var workbook = new Excel.Workbook();
Set Workbook Properties
workbook.creator = "Me";
workbook.lastModifiedBy = "Her";
workbook.created = new Date(1985, 8, 30);
workbook.modified = new Date();
Add a Worksheet
var sheet = workbook.addWorksheet("My Sheet");
Access Worksheets
workbook.eachSheet(function(worksheet, sheetId) {
});
var worksheet = workbook.getWorksheet("My Sheet");
var worksheet = workbook.getWorksheet(1);
Columns
worksheet.columns = [
{ header: "Id", key: "id", width: 10 },
{ header: "Name", key: "name", width: 32 },
{ header: "D.O.B.", key: "DOB", width: 10 }
];
var idCol = worksheet.getColumn("id");
var nameCol = worksheet.getColumn("B");
var dobCol = worksheet.getColumn(3);
dobCol.header = "Date of Birth";
dobCol.header = ["Date of Birth", "A.K.A. D.O.B."];
dobCol.key = "dob";
dobCol.width = 15;
dobCol.eachCell(function(cell, rowNumber) {
});
dobCol.eachCell({ includeEmpty: true }, function(cell, rowNumber) {
});
Rows
worksheet.addRow({id: 1, name: "John Doe", dob: new Date(1970,1,1)});
worksheet.addRow({id: 2, name: "Jane Doe", dob: new Date(1965,1,7)});
worksheet.addRow([3, "Sam", new Date()]);
var rowValues = [];
rowValues[1] = 4;
rowValues[5] = "Kyle";
rowValues[9] = new Date();
worksheet.addRow(rowValues);
var row = worksheet.getRow(5);
var row = worksheet.lastRow;
row.height = 42.5;
row.getCell(1).value = 5;
row.getCell("name").value = "Zeb";
row.getCell("C").value = new Date();
row = worksheet.getRow(4).values;
expect(row[5]).toEqual("Kyle");
row.values = [1,2,3];
expect(row.getCell(1).value).toEqual(1);
expect(row.getCell(2).value).toEqual(2);
expect(row.getCell(3).value).toEqual(3);
var values = []
values[5] = 7;
values[10] = "Hello, World!";
row.values = values;
expect(row.getCell(1).value).toBeNull();
expect(row.getCell(5).value).toEqual(7);
expect(row.getCell(10).value).toEqual("Hello, World!");
row.values = {
id: 13,
name: "Thing 1",
dob: new Date()
};
worksheet.eachRow(function(row, rowNumber) {
console.log("Row " + rowNumber + " = " + JSON.stringify(row.values));
});
worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
console.log("Row " + rowNumber + " = " + JSON.stringify(row.values));
});
row.eachCell(function(cell, colNumber) {
console.log("Cell " + colNumber + " = " + cell.value);
});
row.eachCell({ includeEmpty: true }, function(cell, colNumber) {
console.log("Cell " + colNumber + " = " + cell.value);
});
row.commit();
Handling Individual Cells
worksheet.getCell("C3").value = new Date(1968, 5, 1);
expect(worksheet.getCell("C3").type).toEqual(Excel.ValueType.Date);
Merged Cells
worksheet.mergeCells("A4:B5");
worksheet.mergeCells("G10", "H11");
worksheet.mergeCells(10,11,12,13);
worksheet.getCell("B5").value = "Hello, World!";
expect(worksheet.getCell("A4").value).toBe(worksheet.getCell("B5").value);
expect(worksheet.getCell("A4")).toBe(worksheet.getCell("B5").master);
Styles
Cells, Rows and Columns each support a rich set of styles and formats that affect how the cells are displayed.
Styles are set by assigning the following properties:
ws.getCell("A1").numFmt = "0.00%";
ws.columnscolumns = [
{ header: "Id", key: "id", width: 10 },
{ header: "Name", key: "name", width: 32, style: { font: { name: "Arial Black" } } },
{ header: "D.O.B.", key: "DOB", width: 10, style: { numFmt: "dd/mm/yyyy" } }
];
ws.getColumn(3).numFmt = "�#,##0;[Red]-�#,##0";
ws.getRow(2).font = { name: "Comic Sans MS", family: 4, size: 16, underline: "double", bold: true };
When a style is applied to a row or column, it will be applied to all currently existing cells in that row or column.
Also, any new cell that is created will inherit its initial styles from the row and column it belongs to.
If a cell's row and column both define a specific style (e.g. font), the cell will use the row style over the column style.
However if the row and column define different styles (e.g. column.numFmt and row.font), the cell will inherit the font from the row and the numFmt from the column.
Caveat: All the above properties (with the exception of numFmt, which is a string), are JS object structures.
If the same style object is assigned to more than one spreadsheet entity, then each entity will share the same style object.
If the style object is later modified before the spreadsheet is serialized, then all entities referencing that style object will be modified too.
This behaviour is intended to prioritize performance by reducing the number of JS objects created.
If you want the style objects to be independent, you will need to clone them before assigning them.
Also, by default, when a document is read from file (or stream) if spreadsheet entities share similar styles, then they will reference the same style object too.
Number Formats
ws.getCell("A1").value = 1.6;
ws.getCell("A1").numFmt = "# ?/?";
ws.getCell("B1").value = 0.016;
ws.getCell("B1").numFmt = "0.00%";
Fonts
ws.getCell("A1").font = {
name: "Comic Sans MS",
family: 4,
size: 16,
underline: true,
bold: true
};
ws.getCell("A2").font = {
name: "Arial Black",
color: { argb: "FF00FF00" },
family: 2,
size: 14,
italic: true
};
var font = { name: "Arial", size: 12 };
ws.getCell("A3").font = font;
font.size = 20;
Font Property | Description | Example Value(s) |
---|
name | Font name. | "Arial", "Calibri", etc. |
family | Font family. An integer value. | 1,2,3, etc. |
scheme | Font scheme. | "minor", "major", "none" |
charset | Font charset. An integer value. | 1, 2, etc. |
color | Colour description, an object containing an ARGB value. | { argb: "FFFF0000"} |
bold | Font weight | true, false |
italic | Font slope | true, false |
underline | Font underline style | true, false, "none", "single", "double", "singleAccounting", "doubleAccounting" |
strike | Font strikethrough | true, false |
outline | Font outline | true, false |
Alignment
ws.getCell("A1").alignment = { vertical: "top", horizontal: "left" };
ws.getCell("B1").alignment = { vertical: "middle", horizontal: "center" };
ws.getCell("C1").alignment = { vertical: "bottom", horizontal: "right" };
ws.getCell("D1").alignment = { wrapText: true };
ws.getCell("E1").alignment = { indent: 1 };
ws.getCell("F1").alignment = { textRotation: 30 };
ws.getCell("G1").alignment = { textRotation: -45 };
ws.getCell("H1").alignment = { textRotation: "vertical" };
Valid Alignment Property Values
horizontal | vertical | wrapText | indent | readingOrder | textRotation |
---|
left | top | true | integer | rtl | 0 to 90 |
center | middle | false | | ltr | -1 to -90 |
right | bottom | | | | vertical |
fill | distributed | | | | |
justify | justify | | | | |
centerContinuous | | | | | |
distributed | | | | | |
Borders
ws.getCell("A1").border = {
top: {style:"thin"},
left: {style:"thin"},
bottom: {style:"thin"},
right: {style:"thin"}
};
ws.getCell("A3").border = {
top: {style:"double", color: {argb:"FF00FF00"}},
left: {style:"double", color: {argb:"FF00FF00"}},
bottom: {style:"double", color: {argb:"FF00FF00"}},
right: {style:"double", color: {argb:"FF00FF00"}}
};
ws.getCell("A5").border = {
diagonal: {up: true, down: true, style:"thick", color: {argb:"FFFF0000"}}
};
Valid Border Styles
- thin
- dotted
- dashDot
- hair
- dashDotDot
- slantDashDot
- mediumDashed
- mediumDashDotDot
- mediumDashDot
- medium
- double
- thick
Fills
ws.getCell("A1").fill = {
type: "pattern",
pattern:"darkVertical",
fgColor:{argb:"FFFF0000"}
};
ws.getCell("A2").fill = {
type: "pattern",
pattern:"darkTrellis",
fgColor:{argb:"FFFFFF00"},
bgColor:{argb:"FF0000FF"}
};
ws.getCell("A3").fill = {
type: "gradient",
gradient: "angle",
degree: 0,
stops: [
{position:0, color:{argb:"FF0000FF"}},
{position:0.5, color:{argb:"FFFFFFFF"}},
{position:1, color:{argb:"FF0000FF"}}
]
};
ws.getCell("A2").fill = {
type: "gradient",
gradient: "path",
center:{left:0.5,top:0.5},
stops: [
{position:0, color:{argb:"FFFF0000"}},
{position:1, color:{argb:"FF00FF00"}}
]
};
Pattern Fills
Property | Required | Description |
---|
type | Y | Value: "pattern" Specifies this fill uses patterns |
pattern | Y | Specifies type of pattern (see Valid Pattern Types below) |
fgColor | N | Specifies the pattern foreground color. Default is black. |
bgColor | N | Specifies the pattern background color. Default is white. |
Valid Pattern Types
- none
- solid
- darkVertical
- darkGray
- mediumGray
- lightGray
- gray125
- gray0625
- darkHorizontal
- darkVertical
- darkDown
- darkUp
- darkGrid
- darkTrellis
- lightHorizontal
- lightVertical
- lightDown
- lightUp
- lightGrid
- lightTrellis
- lightGrid
Gradient Fills
Property | Required | Description |
---|
type | Y | Value: "gradient" Specifies this fill uses gradients |
gradient | Y | Specifies gradient type. One of ["angle", "path"] |
degree | angle | For "angle" gradient, specifies the direction of the gradient. 0 is from the left to the right. Values from 1 - 359 rotates the direction clockwise |
center | path | For "path" gradient. Specifies the relative coordinates for the start of the path. "left" and "top" values range from 0 to 1 |
stops | Y | Specifies the gradient colour sequence. Is an array of objects containing position and color starting with position 0 and ending with position 1. Intermediatary positions may be used to specify other colours on the path. |
Caveats
Using the interface above it may be possible to create gradient fill effects not possible using the XLSX editor program.
For example, Excel only supports angle gradients of 0, 45, 90 and 135.
Similarly the sequence of stops may also be limited by the UI with positions [0,1] or [0,0.5,1] as the only options.
Take care with this fill to be sure it is supported by the target XLSX viewers.
File I/O
XLSX
Reading XLSX
var workbook = new Excel.Workbook();
workbook.xlsx.readFile(filename)
.then(function() {
});
var workbook = new Excel.Workbook();
stream.pipe(workbook.xlsx.createInputStream());
Writing XLSX
var workbook = createAndFillWorkbook();
workbook.xlsx.writeFile(filename)
.then(function() {
});
workbook.xlsx.write(stream)
.then(function() {
});
CSV
Reading CSV
var workbook = new Excel.Workbook();
workbook.csv.readFile(filename)
.then(function(worksheet) {
});
var workbook = new Excel.Workbook();
workbook.csv.read(stream)
.then(function(worksheet) {
});
var workbook = new Excel.Workbook();
stream.pipe(workbook.csv.createInputStream());
var workbook = new Excel.Workbook();
var options = {
dateFormats: ["DD/MM/YYYY"]
};
workbook.csv.readFile(filename, options)
.then(function(worksheet) {
});
var workbook = new Excel.Workbook();
var options = {
map: function(value, index) {
switch(index) {
case 0:
return value;
case 1:
return new Date(value);
case 2:
return JSON.parse(value);
default:
return parseFloat(value);
}
}
};
workbook.csv.readFile(filename, options)
.then(function(worksheet) {
});
The CSV parser uses fast-csv to read the CSV file.
The options passed into the read functions above is also passed to fast-csv for parsing of the csv data.
Please refer to the fast-csv README.md for details.
Dates are parsed using the npm module moment.
If no dateFormats are supplied, the following are used:
- moment.ISO_8601
- "MM-DD-YYYY"
- "YYYY-MM-DD"
Writing CSV
var workbook = createAndFillWorkbook();
workbook.csv.writeFile(filename)
.then(function() {
});
workbook.csv.write(stream)
.then(function() {
});
var workbook = new Excel.Workbook();
var options = {
dateFormat: "DD/MM/YYYY HH:mm:ss"
};
workbook.csv.readFile(filename, options)
.then(function(worksheet) {
});
var workbook = new Excel.Workbook();
var options = {
map: function(value, index) {
switch(index) {
case 0:
return value;
case 1:
return moment(value).format("YYYY-MM-DD");
case 2:
return value.result;
default:
return value;
}
}
};
workbook.csv.readFile(filename, options)
.then(function(worksheet) {
});
The CSV parser uses fast-csv to write the CSV file.
The options passed into the write functions above is also passed to fast-csv for writing the csv data.
Please refer to the fast-csv README.md for details.
Dates are formatted using the npm module moment.
If no dateFormat is supplied, moment.ISO_8601 is used.
Streaming I/O
The File I/O documented above requires that an entire workbook is built up in memory before the file can be written.
While convenient, it can limit the size of the document due to the amount of memory required.
A streaming writer (or reader) processes the workbook or worksheet data as it is generated,
converting it into file form as it goes. Typically this is much more efficient on memory as the final
memory footprint and even intermediate memory footprints are much more compact than with the document version,
especially when you consider that the row and cell objects are disposed once they are committed.
The interface to the streaming workbook and worksheet is almost the same as the document versions with a few minor practical differences:
- Once a worksheet is added to a workbook, it cannot be removed.
- Once a row is committed, it is no longer accessible since it will have been dropped from the worksheet.
- unMergeCells() is not supported.
Note that it is possible to build the entire workbook without committing any rows.
When the workbook is committed, all added worksheets (including all uncommitted rows) will be automatically committed.
However in this case, little will have been gained over the Document version.
Streaming XLSX
Streaming XLSX Writer
The streaming XLSX writer is available in the ExcelJS.stream.xlsx namespace.
The constructor takes an optional options object with the following fields:
Field | Description |
---|
stream | Specifies a writable stream to write the XLSX workbook to. |
filename | If stream not specified, this field specifies the path to a file to write the XLSX workbook to. |
useSharedStrings | Specifies whether to use shared strings in the workbook. Default is false |
useStyles | Specifies whether to add style information to the workbook. Styles can add some performance overhead. Default is false |
If neither stream nor filename is specified in the options, the workbook writer will create a StreamBuf object
that will store the contents of the XLSX workbook in memory.
This StreamBuf object, which can be accessed via the property workbook.stream, can be used to either
access the bytes directly by stream.read() or to pipe the contents to another stream.
var options = {
filename: "./streamed-workbook.xlsx",
useStyles: true,
useSharedStrings: true
};
var workbook = new Excel.stream.xlsx.WorkbookWriter(options);
In general, the interface to the streaming XLSX writer is the same as the Document workbook (and worksheets)
described above, in fact the row, cell and style objects are the same.
However there are some differences...
Construction
As seen above, the WorkbookWriter will typically require the output stream or file to be specified in the constructor.
Committing Data
When a worksheet row is ready, it should be committed so that the row object and contents can be freed.
Typically this would be done as each row is added...
worksheet.addRow({
id: i,
name: theName,
etc: someOtherDetail
}).commit();
The reason the WorksheetWriter does not commit rows as they are added is to allow cells to be merged across rows:
worksheet.mergeCells("A1:B2");
worksheet.getCell("A1").value = "I am merged";
worksheet.getCell("C1").value = "I am not";
worksheet.getCell("C2").value = "Neither am I";
worksheet.getRow(2).commit();
As each worksheet is completed, it must also be committed:
worksheet.commit();
To complete the XLSX document, the workbook must be committed. If any worksheet in a workbook are uncommitted,
they will be committed automatically as part of the workbook commit.
workbook.commit();
Value Types
The following value types are supported.
Enum Name | Enum(*) | Description | Example Value |
---|
Excel.ValueType.Null | 0 | No value. | null |
Excel.ValueType.Merge | 1 | N/A | N/A |
Excel.ValueType.Number | 2 | A numerical value | 3.14 |
Excel.ValueType.String | 3 | A text value | "Hello, World!" |
Excel.ValueType.Date | 4 | A Date value | new Date() |
Excel.ValueType.Hyperlink | 5 | A hyperlink | { text: "www.mylink.com", hyperlink: "http://www.mylink.com" } |
Excel.ValueType.Formula | 6 | A formula | { formula: "A1+A2", result: 7 } |
Interface Changes
Every effort is made to make a good consistent interface that doesn't break through the versions but regrettably, now and then some things have to change for the greater good.
Interface Breaks in 0.1.0
Worksheet.eachRow
The arguments in the callback function to Worksheet.eachRow have been swapped and changed; it was function(rowNumber,rowValues), now it is function(row, rowNumber) which gives it a look and feel more like the underscore (_.each) function and prioritises the row object over the row number.
Worksheet.getRow
This function has changed from returning a sparse array of cell values to returning a Row object. This enables accessing row properties and will facilitate managing row styles and so on.
The sparse array of cell values is still available via Worksheet.getRow(rowNumber).values;
Interface Breaks in 0.1.1
cell.model
cell.styles renamed to cell.style
Known Issues
Too Many Worksheets Results in Parse Error
There appears to be an issue in one of the dependent libraries (unzip) where too many files causes the following error to be emitted:
invalid signature: 0x80014
In practical terms, this error only seems to arise with over 98 sheets (or 49 sheets with hyperlinks) so it shouldn't affect that many. I will keep an eye on it though.
Release History
Version | Changes |
---|
0.0.9 | |
0.1.0 | |
0.1.1 | - Bug Fixes
- More textual data written properly to xml (including text, hyperlinks, formula results and format codes)
- Better date format code recognition
- Cell Font Style
|
0.1.2 | - Fixed potential race condition on zip write
|
0.1.3 | |
0.1.5 | - Bug Fixes
- Now handles 10 or more worksheets in one workbook
- theme1.xml file properly added and referenced
- Cell Borders
|
0.1.6 | - Bug Fixes
- More compatable theme1.xml included in XLSX file
- Cell Fills
|
0.1.8 | - Bug Fixes
- More compatable theme1.xml included in XLSX file
- Fixed filename case issue
- Cell Fills
|
0.1.9 | - Bug Fixes
- Added docProps files to satisfy Mac Excel users
- Fixed filename case issue
- Fixed worksheet id issue
- Core Workbook Properties
|
0.1.10 | - Bug Fixes
- Handles File Not Found error
- CSV Files
|
0.1.11 | |
0.2.0 | - Streaming XLSX Writer
- At long last ExcelJS can support writing massive XLSX files in a scalable memory efficient manner. Performance has been optimised and even smaller spreadsheets can be faster to write than the document writer. Options have been added to control the use of shared strings and styles as these can both have a considerable effect on performance
- Worksheet.lastRow
- Access the last editable row in a worksheet.
- Row.commit()
- For streaming writers, this method commits the row (and any previous rows) to the stream. Committed rows will no longer be editable (and are typically deleted from the worksheet object). For Document type workbooks, this method has no effect.
|