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.

This feature will not work properly when Store uses lazyLoad
This class requires a 3rd party library to export to XLSX

Configs

16

Common

disabledInstancePlugin
listenersEvents

Other

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
dateFormat: String

Defines how dates in a cell will be formatted

exportAllColumns: Boolean= false

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.

filename: String

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

clientInstancePlugin
localeClassLocalizable
localizableLocalizable

Properties

15

Common

disabledInstancePlugin

Class hierarchy

isExcelExporter: Boolean= truereadonly
Identifies an object as an instance of ExcelExporter class, or subclass thereof.
isExcelExporter: Boolean= truereadonlystatic
Identifies an object as an instance of ExcelExporter class, or subclass thereof.
isEventsEvents
isInstancePluginInstancePlugin
isLocalizableLocalizable

Lifecycle

configBase

Misc

clientInstancePlugin
localeHelperLocalizable
localeManagerLocalizable

Other

Functions

29

Other

Generate and download an Excel (.xlsx), or CSV file (.csv).

ParameterTypeDescription
configObject

Optional configuration object, which overrides initial settings of the feature/exporter.

config.filenameString

Name of the exported file

config.dateFormatString

Defines how dates in a cell will be formatted

config.csvBoolean | Object

Set to true to output as a CSV file, or as an object where you can specify delimiters.

config.csv.columnDelimiterString

The CSV delimiter to separate values on one line, defaults to ,.

config.csv.lineDelimiterString

The CSV delimiter to separate lines, defaults to \n.

config.columnsString[] | Object[]

An array of column configuration objects

config.rowsModel[]

An array of records to export

Returns: Promise -

Promise that resolves when the export is completed

LstaticLocalizable
onEvents
relayAllEvents
triggerEvents
unEvents

Configuration

applyDefaultsstaticBase

Events

Lifecycle

destroystaticBase

Misc

doDisableInstancePlugin
initClassstaticBase
isOfTypeNamestaticBase
mixinstaticBase
optionalLstaticLocalizable

Events

7

Fires 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 }) => {

});
ParameterTypeDescription
configExportConfig

Export config

columnsColumn[]

An array of columns to export

rowsModel[]

An array of records to export

lineDelimiterString

The CSV delimiter to separate lines

columnDelimiterString

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 }) => {

});
ParameterTypeDescription
configExportConfig

Export config

columnsXLSColumn[]

An array of columns to export

rowsXLSCell[][]

An array of records to export

catchAllEvents
destroyEvents
disableInstancePlugin
enableInstancePlugin

Event handlers

7

Called on the owning Grid before CSV export starts. Return false to cancel the export.

new ExcelExporter({
    onBeforeCSVExport({ config, columns, rows, lineDelimiter, columnDelimiter }) {

    }
});
ParameterTypeDescription
configExportConfig

Export config

columnsColumn[]

An array of columns to export

rowsModel[]

An array of records to export

lineDelimiterString

The CSV delimiter to separate lines

columnDelimiterString

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 }) {

    }
});
ParameterTypeDescription
configExportConfig

Export config

columnsXLSColumn[]

An array of columns to export

rowsXLSCell[][]

An array of records to export

onDestroyEvents
onDisableInstancePlugin
onEnableInstancePlugin

Typedefs

2

Object describing config param for beforeExcelExport and beforeCSVExport event.

ParameterTypeDescription
convertEmptyValueToEmptyStringBoolean

If this is true, exporter will convert all empty values to ''

dateFormatString

Defines how dates in a cell will be formatted

exporterConfigObject

Configuration object for exporter class

exporterClassTableExporter

Exporter class to use as a data provider.

filenameString

Name of the exported file

csvBoolean | Object

Set to true to output as a CSV file, or as an object where you can specify delimiters.