@progress/kendo-angular-excel-export
Version:
Kendo UI for Angular Excel Export component
215 lines (214 loc) • 9.01 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
// eslint-disable max-line-length
import { Component, ContentChildren, Input, QueryList, NgZone } from '@angular/core';
import { saveAs } from '@progress/kendo-file-saver';
import { workbookOptions, toDataURL, isWorkbookOptions } from './ooxml/workbook';
import { ColumnBase } from './columns/column-base';
import { LocalizationService, L10N_PREFIX } from '@progress/kendo-angular-l10n';
import { validatePackage } from '@progress/kendo-licensing';
import { packageMetadata } from './package-metadata';
import * as i0 from "@angular/core";
import * as i1 from "@progress/kendo-angular-l10n";
/**
* Represents the [Kendo UI Excel Export component for Angular]({% slug overview_excelexport %}).
* Configures the settings for the Excel export of the Kendo UI Grid.
*/
export class ExcelExportComponent {
localization;
zone;
/**
* Specifies the name of the file that is exported to Excel.
* @default "Export.xlsx"
*/
fileName = 'Export.xlsx';
/**
* Enables or disables the column filtering in the Excel file
* ([see example]({% slug filtering_excelexport %})).
*/
filterable;
/**
* Specifies if groups are collapsible in the excel file.
*/
collapsible;
/**
* The author of the workbook.
*/
creator;
/**
* The date on which the workbook is created.
* The default value is `new Date()`.
*/
date;
/**
* If `forceProxy` is set to `true`, the content is forwarded to `proxyURL`
* even if the browser supports the local saving of files.
*/
forceProxy;
/**
* The URL of the server-side proxy which will stream the file to the end user. When the browser is not
* capable of saving files locally—for example, Internet Explorer 9 and earlier, and Safari—a
* proxy is used. The implementation of the server-side proxy has to be done by you.
*
* The proxy receives a POST request with the following parameters in the request body:
* - `contentType`—The MIME type of the file.
* - `base64`—The base-64 encoded file content.
* - `fileName`—The file name, as requested by the caller.
* The proxy is expected to return the decoded file with the **Content-Disposition** header set to `attachment; filename="<fileName.xslx>"`.
*/
proxyURL;
/**
* The exported data. If grouped, the data must be structured as described by the
* [`GroupResult`]({% slug api_kendo-data-query_groupresult %}) option of the Kendo UI Data Query component.
*/
data;
/**
* The exported data groups. The groups must be compatible with the
* [`GroupDescriptor`]({% slug api_kendo-data-query_groupdescriptor %}) option of the Kendo UI Data Query component.
*/
group;
/**
* The options of the cells that are inserted before the data, group, and footer cells
* to indicate the group hierarchy when the data is grouped
* ([see example]({% slug cells_excelexport %}#toc-padding-cells)).
*/
paddingCellOptions;
/**
* The options of the cells that are inserted before the header cells
* to align the headers and the column values when the data is grouped
* ([see example]({% slug cells_excelexport %}#toc-header-padding-cells)).
*/
headerPaddingCellOptions;
/**
* @hidden
*/
columns = new QueryList();
constructor(localization, zone) {
this.localization = localization;
this.zone = zone;
validatePackage(packageMetadata);
this.saveFile = this.saveFile.bind(this);
}
/**
* Saves the data to Excel.
*
* @param exportData - An optional parameter. Can be the data that will be exported or [`WorkbookOptions`]({% slug api_excel-export_workbookoptions %}).
*/
save(exportData) {
this.toDataURL(exportData).then(this.saveFile);
}
/**
* Based on the specified columns and data, returns
* [`WorkbookOptions`]({% slug api_excel-export_workbookoptions %})
* ([see example]({% slug customrowsandcells_excelexport %})).
*
* @param exportData - The optional data to be exported.
* @returns {WorkbookOptions} - The workbook options.
*/
workbookOptions(exportData) {
const currentData = this.getExportData(exportData);
const options = workbookOptions({
columns: this.columns,
data: currentData.data,
group: currentData.group,
filterable: this.filterable,
creator: this.creator,
date: this.date,
rtl: this.localization.rtl,
paddingCellOptions: this.paddingCellOptions,
headerPaddingCellOptions: this.headerPaddingCellOptions,
collapsible: this.collapsible
});
return options;
}
/**
* Returns a promise which will be resolved with the file data URI
* ([see example]({% slug filesaving_excelexport %})).
*
* @param exportData - The optional data or [`WorkbookOptions`]({% slug api_excel-export_workbookoptions %}) that will be used to generate the data URI.
* @returns {Promise<string>} - The promise that will be resolved by the file data URI.
*/
toDataURL(exportData) {
const options = isWorkbookOptions(exportData) ?
exportData :
this.workbookOptions(exportData);
return this.zone.runOutsideAngular(() => toDataURL(options));
}
getExportData(exportData) {
let result;
if (exportData) {
if (Array.isArray(exportData)) {
result = {
data: exportData
};
}
else {
result = exportData;
}
}
else {
result = {
data: this.data,
group: this.group
};
}
return result;
}
saveFile(dataURL) {
saveAs(dataURL, this.fileName, {
forceProxy: this.forceProxy,
proxyURL: this.proxyURL
});
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ExcelExportComponent, deps: [{ token: i1.LocalizationService }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: ExcelExportComponent, isStandalone: true, selector: "kendo-excelexport", inputs: { fileName: "fileName", filterable: "filterable", collapsible: "collapsible", creator: "creator", date: "date", forceProxy: "forceProxy", proxyURL: "proxyURL", data: "data", group: "group", paddingCellOptions: "paddingCellOptions", headerPaddingCellOptions: "headerPaddingCellOptions" }, providers: [
LocalizationService,
{
provide: L10N_PREFIX,
useValue: 'kendo.excelexport'
}
], queries: [{ propertyName: "columns", predicate: ColumnBase, descendants: true }], exportAs: ["kendoExcelExport"], ngImport: i0, template: ``, isInline: true });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ExcelExportComponent, decorators: [{
type: Component,
args: [{
exportAs: 'kendoExcelExport',
selector: 'kendo-excelexport',
providers: [
LocalizationService,
{
provide: L10N_PREFIX,
useValue: 'kendo.excelexport'
}
],
template: ``,
standalone: true
}]
}], ctorParameters: function () { return [{ type: i1.LocalizationService }, { type: i0.NgZone }]; }, propDecorators: { fileName: [{
type: Input
}], filterable: [{
type: Input
}], collapsible: [{
type: Input
}], creator: [{
type: Input
}], date: [{
type: Input
}], forceProxy: [{
type: Input
}], proxyURL: [{
type: Input
}], data: [{
type: Input
}], group: [{
type: Input
}], paddingCellOptions: [{
type: Input
}], headerPaddingCellOptions: [{
type: Input
}], columns: [{
type: ContentChildren,
args: [ColumnBase, { descendants: true }]
}] } });