Skip to content

Commit be6b2d3

Browse files
committed
fix(exporters): use an array for processing data and fix file corruption #6673
1 parent 9721552 commit be6b2d3

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,34 +54,19 @@ export class IgxExcelExporterService extends IgxBaseExporter {
5454
@Output()
5555
public onExportEnded = new EventEmitter<IExcelExportEndedEventArgs>();
5656

57-
private static populateFolder(folder: IExcelFolder, zip: JSZip, worksheetData: WorksheetData): any {
57+
private static async populateFolderAsync(folder: IExcelFolder, zip: JSZip, worksheetData: WorksheetData) {
5858
for (const childFolder of folder.childFolders(worksheetData)) {
5959
const folderInstance = ExcelElementsFactory.getExcelFolder(childFolder);
6060
const zipFolder = zip.folder(folderInstance.folderName);
61-
IgxExcelExporterService.populateFolder(folderInstance, zipFolder, worksheetData);
62-
}
63-
64-
for (const childFile of folder.childFiles(worksheetData)) {
65-
const fileInstance = ExcelElementsFactory.getExcelFile(childFile);
66-
if (!(fileInstance instanceof WorksheetFile)) {
67-
fileInstance.writeElement(zip, worksheetData);
68-
}
69-
}
70-
}
71-
72-
private static populateFolderAsync(folder: IExcelFolder, zip: JSZip, worksheetData: WorksheetData, done: () => void): any {
73-
for (const childFolder of folder.childFolders(worksheetData)) {
74-
const folderInstance = ExcelElementsFactory.getExcelFolder(childFolder);
75-
const zipFolder = zip.folder(folderInstance.folderName);
76-
IgxExcelExporterService.populateFolderAsync(folderInstance, zipFolder, worksheetData, done);
61+
await IgxExcelExporterService.populateFolderAsync(folderInstance, zipFolder, worksheetData);
7762
}
7863

7964
for (const childFile of folder.childFiles(worksheetData)) {
8065
const fileInstance = ExcelElementsFactory.getExcelFile(childFile);
8166
if (fileInstance instanceof WorksheetFile) {
82-
(fileInstance as WorksheetFile).writeElementAsync(zip, worksheetData, () => {
83-
done();
84-
});
67+
await (fileInstance as WorksheetFile).writeElementAsync(zip, worksheetData);
68+
} else {
69+
fileInstance.writeElement(zip, worksheetData);
8570
}
8671
}
8772
}
@@ -97,13 +82,14 @@ export class IgxExcelExporterService extends IgxBaseExporter {
9782
}
9883
}
9984

85+
10086
const worksheetData = new WorksheetData(data, options, this._indexOfLastPinnedColumn, this._sort, this._isTreeGrid);
10187
this._xlsx = new JSZip();
10288

10389
const rootFolder = ExcelElementsFactory.getExcelFolder(ExcelFolderTypes.RootExcelFolder);
10490

105-
IgxExcelExporterService.populateFolder(rootFolder, this._xlsx, worksheetData);
106-
IgxExcelExporterService.populateFolderAsync(rootFolder, this._xlsx, worksheetData, () => {
91+
IgxExcelExporterService.populateFolderAsync(rootFolder, this._xlsx, worksheetData)
92+
.then(() => {
10793
this._xlsx.generateAsync(IgxExcelExporterService.ZIP_OPTIONS).then((result) => {
10894
this.saveFile(result, options.fileName);
10995
this.onExportEnded.emit({ xlsx: this._xlsx });

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,14 @@ export class WorksheetFile implements IExcelFile {
136136
worksheetData.isTreeGridData, maxOutlineLevel));
137137
}
138138

139-
public writeElementAsync(folder: JSZip, worksheetData: WorksheetData, done: () => void) {
140-
this.prepareDataAsync(worksheetData, (cols, rows) => {
141-
const hasTable = !worksheetData.isEmpty && worksheetData.options.exportAsTable;
142-
folder.file('sheet1.xml', ExcelStrings.getSheetXML(
143-
this.dimension, this.freezePane, cols, rows, hasTable, worksheetData.isTreeGridData, this.maxOutlineLevel));
144-
done();
139+
public async writeElementAsync(folder: JSZip, worksheetData: WorksheetData) {
140+
return new Promise(resolve => {
141+
this.prepareDataAsync(worksheetData, (cols, rows) => {
142+
const hasTable = !worksheetData.isEmpty && worksheetData.options.exportAsTable;
143+
folder.file('sheet1.xml', ExcelStrings.getSheetXML(
144+
this.dimension, this.freezePane, cols, rows, hasTable, worksheetData.isTreeGridData, this.maxOutlineLevel));
145+
resolve();
146+
});
145147
});
146148
}
147149

@@ -199,16 +201,16 @@ export class WorksheetFile implements IExcelFile {
199201
}
200202

201203
private processDataRecordsAsync(worksheetData: WorksheetData, done: (rows: string) => void) {
202-
let dataRecords = '';
204+
const rowDataArr = new Array(worksheetData.rowCount - 1);
203205
const height = worksheetData.options.rowHeight;
204206
this.rowHeight = height ? ' ht="' + height + '" customHeight="1"' : '';
205207

206208
yieldingLoop(worksheetData.rowCount - 1, 1000,
207209
(i) => {
208-
dataRecords += this.processRow(worksheetData, i + 1);
210+
rowDataArr[i] = this.processRow(worksheetData, i + 1);
209211
},
210212
() => {
211-
done(dataRecords);
213+
done(rowDataArr.join(''));
212214
});
213215
}
214216

0 commit comments

Comments
 (0)