angular 6 export array to excel code example
Example: angular array export to excel
download(){
var csvData = this.ConvertToCSV( this.data);
var a = document.createElement("a");
a.setAttribute('style', 'display:none;');
document.body.appendChild(a);
var blob = new Blob([csvData], { type: 'text/csv' });
var url= window.URL.createObjectURL(blob);
a.href = url;
var x:Date = new Date();
var link:string ="filename_" + x.getMonth() + "_" + x.getDay() + '.csv';
a.download = link.toLocaleLowerCase();
a.click();
}
ConvertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
var row = "";
for (var index in objArray[0]) {
row += index + ',';
}
row = row.slice(0, -1);
str += row + '\r\n';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}