excelHtml5
Create and save an Excel XLSX file that contains the data from the table (HTML5).
Please note - this property requires the Buttons extension for DataTables.
Description
This button provides the end user with the ability to save the table's data into a locally created Excel XLSX file.
The JSZip library must be available on the page or registered with DataTable.Buttons.jszip()
if you are importing modules. JSZip is an MIT licensed library provides the ability to create a ZIP file in the browser, which is required to build a valid XLSX file.
At this time, although an XLSX file is created, data formatting, colours, etc are not retained. Only the raw data from the table is included in the exported file. For complete control over the generated file, a custom button could be constructed using the SheetJS library.
If your table has a header or footer with multiple rows, these will all be included in the export. If the header or footer contains colspan
or rowspan
cells, they will automatically be migrated to the Excel document for export.
Customisation
The Excel file that Buttons creates is intentionally quite simple - the default styling is:
- Calibri font, size 11 (matching Excel default)
- Header and footer are bold
- Column widths are auto sized to fit their contents (min: 5, max: 52)
However, you may wish to add additional information or formatting to the document to suit your output requirements. This ability is provided by the customize
option of this button type.
The customize
method is passed a single parameter - an object with the following structure (note that xml
is simply a place holder to represent an XML document - each XML document is of course different):
{
"_rels": {
".rels": xml
},
"xl": {
"_rels": {
"workbook.xml.rels": xml
},
"workbook.xml": xml,
"styles.xml": xml,
"worksheets": {
"sheet1.xml": xml
}
},
"[Content_Types].xml": xml
}
If you've developed with XLSX files before you will notice that this object's structure mimics the file structure of the XLSX file. When zipped this file structure will create an XLSX file - which is exactly what this button type does. The customize
method provides you with the ability to modify any of the XML documents inside it, and even to add extra files (these are automatically detected in the structure and will be included in the zip).
As an example, let's modify the text shown in cell A1:
customize: function ( xlsx ) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('c[r=A1] t', sheet).text( 'Custom text' );
}
On line two we get the XML document used for the spreadsheet's data. Then on line four a little bit of jQuery is used to select the correct cell's text node (the r
attribute of the c
element is where the cell will be shown and the t
element is the text node). Then set the text for the cell. You could just as readily use DOM methods if you prefer.
We can use the a similar method to customize the styling of the cells in the document. This is done by adding the s
attribute to the c
element(s), where the attribute value is the style index you wish to use. The XLSX file created by Buttons has a number of built in styles which are documented below.
This is only a brief summary of how to customise the XLSX files. Full details of the XLSX file format and its features are outside the scope of this documentation. Please refer to the Microsoft and Office Open XML documentation for details.
Built in styles
The following indexes are available form the styles that are predefined in the Editor XLSX style file. These indexes can be applied to any cells in the generated spreadsheet, altering their appearance.
0
- Normal text1
- White text2
- Bold3
- Italic4
- Underline5
- Normal text, grey background6
- White text, grey background7
- Bold, grey background8
- Italic, grey background9
- Underline, grey background10
- Normal text, red background11
- White text, red background12
- Bold, red background13
- Italic, red background14
- Underline, red background15
- Normal text, green background16
- White text, green background17
- Bold, green background18
- Italic, green background19
- Underline, green background20
- Normal text, blue background21
- White text, blue background22
- Bold, blue background23
- Italic, blue background24
- Underline, blue background25
- Normal text, thin black border26
- White text, thin black border27
- Bold, thin black border28
- Italic, thin black border29
- Underline, thin black border30
- Normal text, grey background, thin black border31
- White text, grey background, thin black border32
- Bold, grey background, thin black border33
- Italic, grey background, thin black border34
- Underline, grey background, thin black border35
- Normal text, red background, thin black border36
- White text, red background, thin black border37
- Bold, red background, thin black border38
- Italic, red background, thin black border39
- Underline, red background, thin black border40
- Normal text, green background, thin black border41
- White text, green background, thin black border42
- Bold, green background, thin black border43
- Italic, green background, thin black border44
- Underline, green background, thin black border45
- Normal text, blue background, thin black border46
- White text, blue background, thin black border47
- Bold, blue background, thin black border48
- Italic, blue background, thin black border49
- Underline, blue background, thin black border50
- Left aligned text (since 1.2.2)51
- Centred text (since 1.2.2)52
- Right aligned text (since 1.2.2)53
- Justified text (since 1.2.2)54
- Text rotated 90° (since 1.2.2)55
- Wrapped text (since 1.2.2)56
- Percentage integer value (automatically detected and used by buttons - since 1.2.3)57
- Dollar currency values (automatically detected and used by buttons - since 1.2.3)58
- Pound currency values (automatically detected and used by buttons - since 1.2.3)59
- Euro currency values (automatically detected and used by buttons - since 1.2.3)60
- Percentage with 1 decimal place (automatically detected and used by buttons - since 1.2.3)61
- Negative numbers indicated by brackets (automatically detected and used by buttons - since 1.2.3)62
- Negative numbers indicated by brackets - 2 decimal places (automatically detected and used by buttons - since 1.2.3)63
- Numbers with thousand separators (automatically detected and used by buttons - since 1.2.3)64
- Numbers with thousand separators - 2 decimal places (automatically detected and used by buttons - since 1.2.3)65
- Numbers without thousand separators (automatically detected and used by buttons - since 1.2.4)66
- Numbers without thousand separators - 2 decimal places (automatically detected and used by buttons - since 1.2.4)
Notes:
- Grey is #d9d9d9
- Red is #d99795
- Green is #6efce
- Blue is #c6cfef
Options
This button can have the following options set in its configuration object to customise its actions and display, in addition to those options which are available for all buttons (e.g. buttons.buttons.text
):
Name | Type | Default |
---|---|---|
action | ||
Create and save a Excel XLSX file. | ||
autoFilter Since: 1.5.4 | false | |
Enable Excel's auto filter feature for the header cells in the table allowing the user to quickly filter and sort the exported spreadsheet in Excel. Note that this does not operate in LibreOffice (although the spreadsheet is still readable). | ||
className | buttons-excel buttons-html5 | |
The button's class name. See | ||
createEmptyCells Since: 1.5.0 | false | |
Option to instruct the Excel export to create empty cells. By default if a cell contains | ||
customize Since: 1.2.0 | undefined | |
This method can be used to modify the XLSX file that is created by Buttons. The first parameter that is passed in is an object that contains the XML files and the object structure matches the file system paths for those files in an XLSX file. Customisation of the XLSX file is a complex topic - please refer to the Customisation section in the As of Buttons 1.5.2 this function is passed three parameters:
| ||
exportOptions | {} | |
Select the data to be gathered from the DataTable for export. This includes options for which columns, rows, ordering and search. Please see the | ||
extension | .xlsx | |
The extension to give the created file name. | ||
filename | * | |
File name to give the created file (plus the extension defined by the | ||
footer | true | |
Indicate if the table footer should be included in the exported data or not. Please note that the default for this parameter was updated in Buttons 3.0 to be | ||
header | true | |
Indicate if the table header should be included in the exported data or not. | ||
messageBottom Since: 1.4.0 | * | |
Message to be shown at the bottom of the table, or the | ||
messageTop Since: 1.4.0 | * | |
Message to be shown at the top of the table, or the | ||
sheetName | Sheet1 | |
Name for the worksheet in Excel file created. The characters [] \ /: *? : are not allowed and will be removed if present. | ||
text | Excel | |
The button's display text. The text can be configured using this option (see | ||
title Since: 1.4.0 | * | |
Title of the table that will be included in the exported data. Please see |
Examples
DataTables initialisation: Use the HTML5 Excel button:
new DataTable('#myTable', {
layout: {
topStart: {
buttons: ['excelHtml5']
}
}
});
DataTables initialisation: Use the excel
button type to alias the HTML button options.:
new DataTable('#myTable', {
layout: {
topStart: {
buttons: ['excel']
}
}
});
DataTables initialisation: Use the exportOptions
to save only the data shown on the current DataTable page:
new DataTable('#myTable', {
layout: {
topStart: {
buttons: [
{
extend: 'excelHtml5',
text: 'Save current page',
exportOptions: {
modifier: {
page: 'current'
}
}
}
]
}
}
});
Enable the auto filter option in Excel:
new DataTable('#myTable', {
layout: {
topStart: {
buttons: [
{
extend: 'excelHtml5',
autoFilter: true
}
]
}
}
});