forked from DevExpress/DevExtreme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.component.ts
More file actions
129 lines (114 loc) · 4.01 KB
/
app.component.ts
File metadata and controls
129 lines (114 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { bootstrapApplication } from '@angular/platform-browser';
import { Component, enableProdMode, provideZoneChangeDetection } from '@angular/core';
import { DxCheckBoxModule } from 'devextreme-angular';
import { Workbook, WorksheetViewFrozen } from 'devextreme-exceljs-fork';
import { saveAs } from 'file-saver-es';
// Our demo infrastructure requires us to use 'file-saver-es'. We recommend that you use the official 'file-saver' package in your applications.
import { exportPivotGrid } from 'devextreme/excel_exporter';
import { Options as DataSourceConfig } from 'devextreme/ui/pivot_grid/data_source';
import { DxPivotGridModule, DxPivotGridTypes } from 'devextreme-angular/ui/pivot-grid';
import { Service, Sale } from './app.service';
if (!/localhost/.test(document.location.host)) {
enableProdMode();
}
let modulePrefix = '';
// @ts-ignore
if (window && window.config?.packageConfigPaths) {
modulePrefix = '/app';
}
@Component({
selector: 'demo-app',
templateUrl: `.${modulePrefix}/app.component.html`,
styleUrls: [`.${modulePrefix}/app.component.css`],
providers: [Service],
preserveWhitespaces: true,
imports: [
DxPivotGridModule,
DxCheckBoxModule,
],
})
export class AppComponent {
sales: Sale[];
dataSource: DataSourceConfig;
exportDataFieldHeaders = false;
exportRowFieldHeaders = false;
exportColumnFieldHeaders = false;
exportFilterFieldHeaders = false;
constructor(service: Service) {
this.dataSource = {
fields: [{
caption: 'Region',
width: 120,
dataField: 'region',
area: 'row',
expanded: true,
}, {
caption: 'City',
dataField: 'city',
width: 150,
area: 'row',
}, {
dataField: 'date',
dataType: 'date',
area: 'column',
filterValues: [[2023], [2024], [2025]],
expanded: false,
}, {
caption: 'Sales',
dataField: 'amount',
dataType: 'number',
summaryType: 'sum',
format: 'currency',
area: 'data',
}, {
caption: 'Country',
dataField: 'country',
area: 'filter',
}],
store: service.getSales(),
};
}
onExporting(e: DxPivotGridTypes.ExportingEvent) {
const workbook = new Workbook();
const worksheet = workbook.addWorksheet('Sales');
worksheet.columns = [
{ width: 30 }, { width: 20 }, { width: 30 }, { width: 30 }, { width: 30 }, { width: 30 },
];
exportPivotGrid({
component: e.component,
worksheet,
topLeftCell: { row: 4, column: 1 },
keepColumnWidths: false,
exportDataFieldHeaders: this.exportDataFieldHeaders,
exportRowFieldHeaders: this.exportRowFieldHeaders,
exportColumnFieldHeaders: this.exportColumnFieldHeaders,
exportFilterFieldHeaders: this.exportFilterFieldHeaders,
}).then((cellRange) => {
// Header
const headerRow = worksheet.getRow(2);
headerRow.height = 30;
const columnFromIndex = (worksheet.views[0] as WorksheetViewFrozen).xSplit + 1;
const columnToIndex = columnFromIndex + 3;
worksheet.mergeCells(2, columnFromIndex, 2, columnToIndex);
const headerCell = headerRow.getCell(columnFromIndex);
headerCell.value = 'Sales Amount by Region';
headerCell.font = { name: 'Segoe UI Light', size: 22, bold: true };
headerCell.alignment = { horizontal: 'left', vertical: 'middle', wrapText: true };
// Footer
const footerRowIndex = cellRange.to.row + 2;
const footerCell = worksheet.getRow(footerRowIndex).getCell(cellRange.to.column);
footerCell.value = 'www.wikipedia.org';
footerCell.font = { color: { argb: 'BFBFBF' }, italic: true };
footerCell.alignment = { horizontal: 'right' };
}).then(() => {
workbook.xlsx.writeBuffer().then((buffer) => {
saveAs(new Blob([buffer], { type: 'application/octet-stream' }), 'Sales.xlsx');
});
});
}
}
bootstrapApplication(AppComponent, {
providers: [
provideZoneChangeDetection({ eventCoalescing: true, runCoalescing: true }),
],
});