Add table csv export
parent
c8742dd9ea
commit
65f4a026ab
@ -1,32 +1,63 @@
|
|||||||
import { Injectable } from "@angular/core";
|
import { Injectable } from '@angular/core';
|
||||||
import * as FileSaver from 'file-saver';
|
import * as FileSaver from 'file-saver';
|
||||||
|
|
||||||
const EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
|
const EXCEL_TYPE =
|
||||||
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
|
||||||
const JSON_TYPE = 'data:text/json;charset=UTF-8';
|
const JSON_TYPE = 'data:text/json;charset=UTF-8';
|
||||||
|
const CSV_TYPE = 'text/csv;charset=utf-8';
|
||||||
|
|
||||||
const EXCEL_EXTENSION = '.xlsx';
|
const EXCEL_EXTENSION = '.xlsx';
|
||||||
const JSON_EXTENSION = '.json';
|
const JSON_EXTENSION = '.json';
|
||||||
|
const CSV_EXTENSION = '.csv';
|
||||||
|
|
||||||
|
@Injectable({ providedIn: 'root' })
|
||||||
@Injectable({ providedIn: "root" })
|
|
||||||
export class DownloadService {
|
export class DownloadService {
|
||||||
csvChar: string = ";";
|
csvChar: string = ';';
|
||||||
|
|
||||||
|
public saveTableDataAsCSV(tableData: any, columnInfo: any, fileName: string) {
|
||||||
/*public exportAsExcelFile(json: any[], excelFileName: string, sheetName: string): void {
|
const csvData = [];
|
||||||
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
|
const header = columnInfo
|
||||||
const workbook: XLSX.WorkBook = { Sheets: { [sheetName] : worksheet }, SheetNames: [sheetName] };
|
.map((column) => column.translation)
|
||||||
const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
|
.join(this.csvChar);
|
||||||
this.saveAsExcelFile(excelBuffer, excelFileName);
|
csvData.push(header);
|
||||||
}*/
|
for (const row of tableData) {
|
||||||
|
const newRow = columnInfo.map((column) => {
|
||||||
|
let cellData = row[column.dataPath];
|
||||||
|
if (Array.isArray(cellData)) {
|
||||||
|
cellData = cellData.join(', ');
|
||||||
|
} else if (column.type === 'NumRange') {
|
||||||
|
cellData = (row[column.dataPath + ".min"] || '') + " bis " + (row[column.dataPath + ".max"] || '');
|
||||||
|
} else if (column.type === 'DateRange') {
|
||||||
|
cellData = (row[column.dataPath + ".from"] || '') + " bis " + (row[column.dataPath + ".to"] || '');
|
||||||
|
}
|
||||||
|
return JSON.stringify(cellData || '');
|
||||||
|
});
|
||||||
|
csvData.push(newRow.join(this.csvChar));
|
||||||
|
}
|
||||||
|
const BOM = '\uFEFF';
|
||||||
|
const csv = BOM + csvData.join('\r\n');
|
||||||
|
const blob = new Blob([csv], { type: CSV_TYPE });
|
||||||
|
FileSaver.saveAs(
|
||||||
|
blob,
|
||||||
|
fileName + '_export_' + new Date().toLocaleString() + CSV_EXTENSION
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private saveAsExcelFile(buffer: any, fileName: string): void {
|
private saveAsExcelFile(buffer: any, fileName: string): void {
|
||||||
const data: Blob = new Blob([buffer], {type: EXCEL_TYPE});
|
const data: Blob = new Blob([buffer], { type: EXCEL_TYPE });
|
||||||
FileSaver.saveAs(data, fileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION);
|
FileSaver.saveAs(
|
||||||
|
data,
|
||||||
|
fileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public exportJSONFile(json: any, fileName: string){
|
public exportJSONFile(json: any, fileName: string) {
|
||||||
const data: Blob = new Blob([JSON.stringify(json, null, 2)], {type: JSON_TYPE});
|
const data: Blob = new Blob([JSON.stringify(json, null, 2)], {
|
||||||
FileSaver.saveAs(data, fileName + '_export_' + new Date().getTime() + JSON_EXTENSION);
|
type: JSON_TYPE,
|
||||||
|
});
|
||||||
|
FileSaver.saveAs(
|
||||||
|
data,
|
||||||
|
fileName + '_export_' + new Date().getTime() + JSON_EXTENSION
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue