Skip to content

Export Excel

Module Extensions

HTMLRevoGridElementEventMap (Extended from global)

interface HTMLRevoGridElementEventMap {
/**
* Event for export excel
*
* @example
* ```typescript
* grid.addEventListener(EXCEL_EXPORT_EVENT, (event) => {
* console.log(event);
* });
*/
[EXCEL_EXPORT_EVENT]: ExportExcelEvent | undefined
}

AdditionalData (Extended from @revolist/revogrid)

interface AdditionalData {
/**
* Additional data property for export excel
*
* @example
* ```typescript
* const grid = document.createElement('revo-grid');
* grid.additionalData = { excel: { allowDrag: false, parsingOptions: { raw: true, xlfn: true, dense: true, PRN: true } } };
* ```
*/
excel?: ExcelConfig
}

Plugin API

ExportExcelPlugin

The ExportExcelPlugin is a plugin for RevoGrid that enables seamless import and export of Excel files (.xlsx, .xls) to and from the grid, enhancing data interoperability with external systems.

Features:

  • Drag-and-drop interface for importing Excel files directly into the grid.
  • Event-driven architecture allowing hooks for customization via ‘excel-before-import’ and ‘excel-before-set’ events, giving plugins the ability to modify data before it’s processed by the grid.
  • Export current grid data to an Excel file, with customizable workbook and sheet names.
  • Configurable to allow or restrict drag file import based on user settings.

Usage:

  • Integrate ExportExcelPlugin as part of RevoGrid’s plugin array to enable Excel import/export functionality.
  • Listen to relevant events to extend or customize the data handling behavior.

Example

import { ExportExcelPlugin } from '@revolist/revogrid-pro'
const grid = document.createElement('revo-grid');
grid.plugins = [ExportExcelPlugin];

This plugin is essential for applications that require integration with Excel files, providing an efficient way to manage large datasets and maintain data consistency across different platforms.

class ExportExcelPlugin {
/**
* Imports an Excel file into the grid.
*
* @param file The File object to import
*
* This method reads the file using FileReader, parses it using xlsx library,
* and then dispatches the 'excel-before-import' event to allow plugins to
* modify the sheet before it is imported.
*
* After the sheet is imported, the method dispatches the 'excel-before-set'
* event to allow plugins to modify the data before it is set as the new
* source.
*
* If either event is prevented, the method will not set the new source.
*
* If the file is not an Excel file, the method will log an error.
*/
async importFile(
file: File,
config: ExcelConfig = this.revogrid.additionalData?.excel ||;
isExcel(file: File, allowedExtensions: string[] = ['.xlsx', '.xls']);
export(config?: ExportExcelEvent);
}

BookType

This file defines the ExportExcelEvent type, which is used to configure the export of data from RevoGrid to an Excel file. It includes options for the Excel sheet and workbook names, as well as detailed configuration for writing options such as file type, compression, and workbook properties.

Features:

  • Support for a variety of Excel file types through the BookType union.
  • Customizable properties for the exported Excel file such as title, author, and creation date encapsulated in the Properties interface.
  • Flexible writing options allowing control over data encoding, compression, and error suppression.

This type is essential for applications needing to export grid data to Excel, providing a structured way to specify export settings and ensure compatibility with various Excel file formats.

export-excel/export-excel-event.type.ts
/**
* This file defines the `ExportExcelEvent` type, which is used to configure the export
* of data from RevoGrid to an Excel file. It includes options for the Excel sheet and
* workbook names, as well as detailed configuration for writing options such as file
* type, compression, and workbook properties.
*
* **Features**:
* - Support for a variety of Excel file types through the `BookType` union.
* - Customizable properties for the exported Excel file such as title, author, and
* creation date encapsulated in the `Properties` interface.
* - Flexible writing options allowing control over data encoding, compression, and error
* suppression.
*
* This type is essential for applications needing to export grid data to Excel, providing
* a structured way to specify export settings and ensure compatibility with various Excel
* file formats.
*/
export type BookType = 'xlsx' | 'xlsm' | 'xlsb' | 'xls' | 'xla' | 'biff8' | 'biff5' | 'biff2' | 'xlml' | 'ods' | 'fods' | 'csv' | 'txt' | 'sylk' | 'slk' | 'html' | 'dif' | 'rtf' | 'prn' | 'eth' | 'dbf';

Properties

Basic File Properties

interface Properties {
/** Summary tab "Title" */
Title?: string;
/** Summary tab "Subject" */
Subject?: string;
/** Summary tab "Author" */
Author?: string;
/** Summary tab "Manager" */
Manager?: string;
/** Summary tab "Company" */
Company?: string;
/** Summary tab "Category" */
Category?: string;
/** Summary tab "Keywords" */
Keywords?: string;
/** Summary tab "Comments" */
Comments?: string;
/** Statistics tab "Last saved by" */
LastAuthor?: string;
/** Statistics tab "Created" */
CreatedDate?: Date
}

ExportExcelEvent

Example usage of the ExportExcelEvent type

Example:

* const exportConfig: ExportExcelEvent = {
* sheetName: 'Property Data', // Specify the name of the Excel sheet
* workbookName: 'Properties.xlsx', // Name of the workbook file
* writingOptions: {
* type: 'file', // Output type
* bookType: 'xlsx', // Workbook type
* compression: true, // Enable compression
* ignoreEC: true, // Ignore Excel compatibility
* },
* };
/**
* Example usage of the ExportExcelEvent type
*
* @example
* const exportConfig: ExportExcelEvent = {
* sheetName: 'Property Data', // Specify the name of the Excel sheet
* workbookName: 'Properties.xlsx', // Name of the workbook file
* writingOptions: {
* type: 'file', // Output type
* bookType: 'xlsx', // Workbook type
* compression: true, // Enable compression
* ignoreEC: true, // Ignore Excel compatibility
* },
* };
*/
export type ExportExcelEvent = {
sheetName?: string; // The name of the sheet within the Excel file
workbookName?: string; // The name of the Excel workbook
writingOptions?: {
/** Output data encoding */
type?: 'base64' | 'binary' | 'buffer' | 'file' | 'array' | 'string';
/**
* Generate Shared String Table
* @default false
*/
bookSST?: boolean;
/**
* File format of generated workbook
* @default 'xlsx'
*/
bookType?: BookType;
/**
* Use ZIP compression for ZIP-based formats
* @default false
*/
compression?: boolean;
/**
* Suppress "number stored as text" errors in generated files
* @default true
*/
ignoreEC?: boolean;
/** Override workbook properties on save */
Props?: Properties;
/** Base64 encoding of NUMBERS base for exports */
numbers?: string;
}; // Options for how the file is written
};

ExcelConfig

export type ExcelConfig = {
/** Allow drag and drop files to the grid area, default: true */
allowDrag?: boolean;
/** Excel parsing options */
parsingOptions?: {
/** If true, plaintext parsing will not parse values */
raw?: boolean;
/** If true, preserve _xlfn. prefixes in formula function names */
xlfn?: boolean;
dense?: boolean;
PRN?: boolean;
};
/** Extra options to parse the sheet to the grid format. default: { header: 1 } - define the first row as the header */
jsonFormat?: {
/** Output format */
header?: "A"|number|string[];
/** Override worksheet range */
range?: any;
/** Include or omit blank lines in the output */
blankrows?: boolean;
/** Default value for null/undefined values */
defval?: any;
/** if true, return raw data; if false, return formatted text */
raw?: boolean;
/** if true, skip hidden rows and columns */
skipHidden?: boolean;
/** if true, return raw numbers; if false, return formatted numbers */
rawNumbers?: boolean;
};
/** Skip column generation based on the first row - keep existing columns defined in the grid. default: false */
skipColumnGeneration?: boolean;
/** Allowed file extensions. default: ['.xlsx', '.xls'] */
allowedExtensions?: string[];
};