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
|
/*
* # ============LICENSE_START=======================================================
* # Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
* # ================================================================================
* # Licensed under the Apache License, Version 2.0 (the "License");
* # you may not use this file except in compliance with the License.
* # You may obtain a copy of the License at
* #
* # http://www.apache.org/licenses/LICENSE-2.0
* #
* # Unless required by applicable law or agreed to in writing, software
* # distributed under the License is distributed on an "AS IS" BASIS,
* # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* # See the License for the specific language governing permissions and
* # limitations under the License.
* # ============LICENSE_END=========================================================
*/
import { Injectable } from '@angular/core';
import * as saveAs from 'file-saver';
@Injectable({
providedIn: 'root'
})
export class DownloadService {
constructor() { }
/* * * * Download json file * * * */
downloadJSON(content, fileName){
let file = new Blob([JSON.stringify(content)], { type: 'text;charset=utf-8' });
let name: string = `${fileName}.json`
saveAs(file, name)
}
/* * * * Export ms instance table to excel or csv * * * */
exportTableData(exportTo, downloadElements, arrHeader) {
if (exportTo === "excel") {
import("xlsx").then(xlsx => {
const worksheet = xlsx.utils.json_to_sheet(downloadElements);
const workbook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
const excelBuffer: any = xlsx.write(workbook, { bookType: 'xlsx', type: 'array' });
this.saveAsExcelFile(excelBuffer, "Table_Data");
});
} else if (exportTo === "csv") {
let csvData = this.convertToCSV(downloadElements, arrHeader)
var blob = new Blob([csvData], { type: 'text/csv' })
saveAs(blob, "Table_Data.csv");
}
}
saveAsExcelFile(buffer: any, fileName: string): void {
import("file-saver").then(FileSaver => {
let EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
let EXCEL_EXTENSION = '.xlsx';
const data: Blob = new Blob([buffer], {
type: EXCEL_TYPE
});
FileSaver.saveAs(data, fileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION);
});
}
convertToCSV(objArray, headerList) {
let array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
let str = '';
let row = '';
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 = '';
for (let index in headerList) {
let head = headerList[index];
if (array[i][head] === null || array[i][head] === undefined) {
line += ','
} else {
if (head === "Labels" && array[i][head].length > 1) { line += '[' + array[i][head].join('] [') + '],'; }
else { line += array[i][head] + ','; }
}
}
str += line + '\r\n';
}
return str;
}
}
|