angular array export to excel

Solutions on MaxInterview for angular array export to excel by the best coders in the world

showing results for - "angular array export to excel"
Charly
08 Mar 2019
1//DOWNLOAD
2  download(){
3    var csvData = this.ConvertToCSV( this.data);
4    var a = document.createElement("a");
5    a.setAttribute('style', 'display:none;');
6    document.body.appendChild(a);
7    var blob = new Blob([csvData], { type: 'text/csv' });
8    var url= window.URL.createObjectURL(blob);
9    a.href = url;
10    var x:Date = new Date();
11    var link:string ="filename_" + x.getMonth() +  "_" +  x.getDay() + '.csv';
12    a.download = link.toLocaleLowerCase();
13    a.click();
14
15  }
16
17
18// convert Json to CSV data in Angular2
19  ConvertToCSV(objArray) {
20    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
21    var str = '';
22    var row = "";
23
24    for (var index in objArray[0]) {
25        //Now convert each value to string and comma-separated
26        row += index + ',';
27    }
28    row = row.slice(0, -1);
29    //append Label row with line break
30    str += row + '\r\n';
31
32    for (var i = 0; i < array.length; i++) {
33        var line = '';
34        for (var index in array[i]) {
35            if (line != '') line += ','
36
37            line += array[i][index];
38        }
39        str += line + '\r\n';
40    }
41    return str;
42  }
43