Skip to content

Commit 209b39e

Browse files
committed
fix(excel-exporters): use grid column width #6673
1 parent 43d2404 commit 209b39e

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

projects/igniteui-angular/src/lib/services/excel/excel-exporter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ export class IgxExcelExporterService extends IgxBaseExporter {
8282
}
8383
}
8484

85+
const worksheetData =
86+
new WorksheetData(data, options, this._indexOfLastPinnedColumn, this._sort, this._isTreeGrid, this.columnWidthList);
8587

86-
const worksheetData = new WorksheetData(data, options, this._indexOfLastPinnedColumn, this._sort, this._isTreeGrid);
8788
this._xlsx = new JSZip();
8889

8990
const rootFolder = ExcelElementsFactory.getExcelFolder(ExcelFolderTypes.RootExcelFolder);

projects/igniteui-angular/src/lib/services/excel/worksheet-data-dictionary.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export class WorksheetDataDictionary {
1515
private _keysAreValid: boolean;
1616

1717
private _counter: number;
18-
private _calculateColumnWidth: boolean;
1918
private _columnWidths: number[];
2019
private _context: any;
2120

@@ -25,18 +24,19 @@ export class WorksheetDataDictionary {
2524
public stringsCount: number;
2625

2726
// TODO Switch to fixed column width since auto-calculation takes a lot of time
28-
constructor(columnCount: number, columnWidth: number) {
27+
constructor(columnCount: number, columnWidth: number, columnWidthsList: number[]) {
2928
this._dictionary = {};
3029
this._widthsDictionary = {};
3130
this._counter = 0;
3231
this.dirtyKeyCollections();
3332

34-
this._calculateColumnWidth = !columnWidth;
3533
this._columnWidths = new Array<number>(columnCount);
3634
this._columnTypeInfo = new Array<boolean>(columnCount);
3735

38-
if (!this._calculateColumnWidth) {
36+
if (columnWidth) {
3937
this._columnWidths.fill(columnWidth);
38+
} else {
39+
this._columnWidths = columnWidthsList;
4040
}
4141

4242
this.stringsCount = 0;
@@ -69,12 +69,6 @@ export class WorksheetDataDictionary {
6969
this.hasNonStringValues = true;
7070
}
7171

72-
if (this._calculateColumnWidth) {
73-
const width = this.getTextWidth(value);
74-
const maxWidth = Math.max(this._columnWidths[column] || 0, width);
75-
this._columnWidths[column] = maxWidth;
76-
}
77-
7872
return isSavedAsString ? this.getSanitizedValue(sanitizedValue) : -1;
7973
}
8074

projects/igniteui-angular/src/lib/services/excel/worksheet-data.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export class WorksheetData {
1111
private _isSpecialData: boolean;
1212

1313
constructor(private _data: any[], public options: IgxExcelExporterOptions, public indexOfLastPinnedColumn,
14-
public sort: any, public isTreeGridData = false) {
15-
this.initializeData();
14+
public sort: any, public isTreeGridData = false, public columnWidths: number[]) {
15+
this.initializeData(columnWidths);
1616
}
1717

1818
public get data() {
@@ -43,7 +43,7 @@ export class WorksheetData {
4343
return this._dataDictionary;
4444
}
4545

46-
private initializeData() {
46+
private initializeData(columnWidths: number[]) {
4747
if (!this._data || this._data.length === 0) {
4848
return;
4949
}
@@ -60,6 +60,6 @@ export class WorksheetData {
6060
this._columnCount = this._keys.length;
6161
this._rowCount = this._data.length + 1;
6262

63-
this._dataDictionary = new WorksheetDataDictionary(this._columnCount, this.options.columnWidth);
63+
this._dataDictionary = new WorksheetDataDictionary(this._columnCount, this.options.columnWidth, columnWidths);
6464
}
6565
}

projects/igniteui-angular/src/lib/services/exporter-common/base-export-service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export interface IColumnExportingEventArgs extends IBaseEventArgs {
6969
export abstract class IgxBaseExporter {
7070
private _columnList: any[];
7171
private flatRecords = [];
72+
private _columnWidthList: number[];
7273

7374
protected _isTreeGrid = false;
7475
protected _indexOfLastPinnedColumn = -1;
@@ -96,6 +97,10 @@ export abstract class IgxBaseExporter {
9697
*/
9798
public onColumnExport = new EventEmitter<IColumnExportingEventArgs>();
9899

100+
public get columnWidthList() {
101+
return this._columnWidthList;
102+
}
103+
99104
/**
100105
* Method for exporting IgxGrid component's data.
101106
* ```typescript
@@ -110,6 +115,7 @@ export abstract class IgxBaseExporter {
110115

111116
const columns = grid.columnList.toArray();
112117
this._columnList = new Array<any>(columns.length);
118+
this._columnWidthList = new Array<any>(columns.length);
113119

114120
const hiddenColumns = [];
115121
let lastVisbleColumnIndex = -1;
@@ -118,6 +124,7 @@ export abstract class IgxBaseExporter {
118124
const columnHeader = column.header !== '' ? column.header : column.field;
119125
const exportColumn = !column.hidden || options.ignoreColumnsVisibility;
120126
const index = options.ignoreColumnsOrder ? column.index : column.visibleIndex;
127+
const columnWidth = Number(column.width.slice(0, -2));
121128

122129
const columnInfo = {
123130
header: columnHeader,
@@ -129,6 +136,7 @@ export abstract class IgxBaseExporter {
129136

130137
if (index !== -1) {
131138
this._columnList[index] = columnInfo;
139+
this._columnWidthList[index] = columnWidth;
132140
lastVisbleColumnIndex = Math.max(lastVisbleColumnIndex, index);
133141
} else {
134142
hiddenColumns.push(columnInfo);

0 commit comments

Comments
 (0)