aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/_services/csv-parse.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/app/_services/csv-parse.service.ts')
-rw-r--r--frontend/src/app/_services/csv-parse.service.ts40
1 files changed, 39 insertions, 1 deletions
diff --git a/frontend/src/app/_services/csv-parse.service.ts b/frontend/src/app/_services/csv-parse.service.ts
index 4a05535a..aae10193 100644
--- a/frontend/src/app/_services/csv-parse.service.ts
+++ b/frontend/src/app/_services/csv-parse.service.ts
@@ -1,4 +1,10 @@
import { Injectable } from "@angular/core";
+import * as FileSaver from 'file-saver';
+import * as XLSX from 'xlsx';
+
+const EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
+const EXCEL_EXTENSION = '.xlsx';
+
@Injectable({ providedIn: 'root' })
export class CsvParseService {
@@ -47,10 +53,42 @@ export class CsvParseService {
if (strMatchedValue.length > 0)
arrData[arrData.length - 1].push(strMatchedValue);
- else
+ else
arrData[arrData.length - 1].push(null);
}
return (arrData);
}
+
+ ConvertJSONToCSV(objArray: string, headerList: { [x: string]: any; }) {
+ let array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
+ let str = '';
+ let row = 'S.No,';
+ for (let index in headerList) {
+ row += headerList[index] + ',';
+ }
+ row = row.slice(0, -1);
+ str += row + '\r\n';
+ for (let i = 0; i < array.length; i++) {
+ let line = (i + 1) + '';
+ for (let index in headerList) {
+ let head = headerList[index];
+ line += ',' + array[i][head];
+ }
+ str += line + '\r\n';
+ }
+ return str;
+ }
+
+ public exportAsExcelFile(json: any[], excelFileName: string): void {
+ const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
+ const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
+ const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
+ this.saveAsExcelFile(excelBuffer, excelFileName);
+ }
+
+ private saveAsExcelFile(buffer: any, fileName: string): void {
+ const data: Blob = new Blob([buffer], { type: EXCEL_TYPE });
+ FileSaver.saveAs(data, fileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION);
+ }
} \ No newline at end of file