ExcelExporter
A feature that allows exporting Grid data to Excel or CSV without involving the server. It uses TableExporter class as data provider, 3rd party provider to generate XLS files, and Microsoft XML specification.
import WriteExcelFileProvider from '../../lib/Grid/feature/experimental/xlsproviders/WriteExcelFileProvider.js';
new Grid({
features : {
excelExporter : {
xlsProvider : WriteExcelFileProvider
}
}
});
Implementing custom provider
// Global scope
<script src="https://cdn.jsdelivr.net/npm/write-excel-file/bundle/write-excel-file.min.js"></script>
// importing from package
import writeXlsxFile from 'write-excel-file';
const typeMap = { string : String, number : Number, date : Date };
class MyXlsProvider {
static write({ filename, columns, rows }) {
columns.forEach(col => delete col.type);
rows.forEach(row => row.forEach(cell => cell.type = typeMap[cell.type] || String));
globalThis.writeXlsxFile([columns, ...rows], {
// write-excel-file uses amount of symbols as width, so we need to convert pixels to symbols
columns : columns.map(col => ({ ...col, width : Math.round(col.width / 10) })),
fileName : filename,
dateFormat : 'yyyy-mm-dd'
});
}
}
const grid = new Grid({
features : {
excelExporter : {
xlsProvider : MyXlsProvider
}
}
})
Here is an example of how to add the feature:
const grid = new Grid({
features : {
excelExporter : {
// Choose the date format for date fields
dateFormat : 'YYYY-MM-DD HH:mm',
exporterConfig : {
// Choose the columns to include in the exported file
columns : ['name', 'role'],
// Optional, export only selected rows
rows : grid.selectedRecords
}
}
}
});
And how to call it:
grid.features.excelExporter.export({
filename : 'Export',
exporterConfig : {
columns : [
{ text : 'First Name', field : 'firstName', width : 90 },
{ text : 'Age', field : 'age', width : 40 },
{ text : 'Starts', field : 'start', width : 140 },
{ text : 'Ends', field : 'finish', width : 140 }
]
}
})
Exporting to CSV is done with the csv config:
grid.features.excelExporter.export({
filename : 'myfile',
csv : true
})
This feature is disabled by default. For info on enabling it, see GridFeatures.
Configs
16
Configs
16Other
If this config is true, exporter will convert all empty values to ''. Empty values are:
- undefined, null, NaN
- Objects/class instances that do not have toString method defined and are stringified to [object Object]
- functions
Defines how dates in a cell will be formatted
Set this config to true to export all exportable columns including
hidden columns as well.
Exporter class to use as a data provider. TableExporter by default.
Configuration object for exporter class.
Name of the exported file
This hook allows to use 3rd party libraries to generate XLSX files.
The default provider is the built-in WriteExcelFileProvider.
A custom provider must extend XlsProviderBase and implement a static write method.
Misc
Properties
15
Properties
15Common
Class hierarchy
Other
Functions
29
Functions
29Other
Generate and download an Excel (.xlsx), or CSV file (.csv).
| Parameter | Type | Description |
|---|---|---|
config | Object | Optional configuration object, which overrides initial settings of the feature/exporter. |
config.filename | String | Name of the exported file |
config.dateFormat | String | Defines how dates in a cell will be formatted |
config.csv | Boolean | Object | Set to |
config.csv.columnDelimiter | String | The CSV delimiter to separate values on one line, defaults to |
config.csv.lineDelimiter | String | The CSV delimiter to separate lines, defaults to |
config.columns | String[] | Object[] | An array of column configuration objects |
config.rows | Model[] | An array of records to export |
Promise that resolves when the export is completed
Configuration
Events
Misc
Events
7
Events
7Fires on the owning Grid before CSV export starts. Return false to cancel the export.
// Adding a listener using the "on" method
excelExporter.on('beforeCSVExport', ({ config, columns, rows, lineDelimiter, columnDelimiter }) => {
});| Parameter | Type | Description |
|---|---|---|
config | ExportConfig | Export config |
columns | Column[] | An array of columns to export |
rows | Model[] | An array of records to export |
lineDelimiter | String | The CSV delimiter to separate lines |
columnDelimiter | String | The CSV delimiter to separate values on one line |
Fires on the owning Grid before Excel export starts. Return false to cancel the export.
// Adding a listener using the "on" method
excelExporter.on('beforeExcelExport', ({ config, columns, rows }) => {
});| Parameter | Type | Description |
|---|---|---|
config | ExportConfig | Export config |
columns | XLSColumn[] | An array of columns to export |
rows | XLSCell[][] | An array of records to export |
Event handlers
7
Event handlers
7Called on the owning Grid before CSV export starts. Return false to cancel the export.
new ExcelExporter({
onBeforeCSVExport({ config, columns, rows, lineDelimiter, columnDelimiter }) {
}
});| Parameter | Type | Description |
|---|---|---|
config | ExportConfig | Export config |
columns | Column[] | An array of columns to export |
rows | Model[] | An array of records to export |
lineDelimiter | String | The CSV delimiter to separate lines |
columnDelimiter | String | The CSV delimiter to separate values on one line |
Called on the owning Grid before Excel export starts. Return false to cancel the export.
new ExcelExporter({
onBeforeExcelExport({ config, columns, rows }) {
}
});| Parameter | Type | Description |
|---|---|---|
config | ExportConfig | Export config |
columns | XLSColumn[] | An array of columns to export |
rows | XLSCell[][] | An array of records to export |
Typedefs
2
Typedefs
2Object describing config param for beforeExcelExport and beforeCSVExport event.
| Parameter | Type | Description |
|---|---|---|
convertEmptyValueToEmptyString | Boolean | If this is true, exporter will convert all empty values to |
dateFormat | String | Defines how dates in a cell will be formatted |
exporterConfig | Object | Configuration object for exporter class |
exporterClass | TableExporter | Exporter class to use as a data provider. |
filename | String | Name of the exported file |
csv | Boolean | Object | Set to true to output as a CSV file, or as an object where you can specify delimiters. |