Reputation: 680
I have an array of objects as shown below
[
{ "FirstName": "John",
"LastName": "Parker",
"Age": "23",
"Cat": "23g",
"SOP": "Active"
},
{ "FirstName": "Rose",
"LastName": "Jackson",
"Age": "44",
"Cat": "44g",
"SOP": "InActive"
}
]
how can i export this data along with the logo
of the company to the excel and download the same.
Any suitable plugin to write objects data
and logo image
to excel?
Upvotes: 0
Views: 4544
Reputation: 1905
you can use this function , it works fine with me
//DOWNLOAD
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();
}
// convert Json to CSV data in Angular2
ConvertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
var row = "";
for (var index in objArray[0]) {
//Now convert each value to string and comma-separated
row += index + ',';
}
row = row.slice(0, -1);
//append Label row with line break
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;
}
Upvotes: 6
Reputation: 86800
The better approach is to do this from backend side but still, there are some packages available to do same, check it out
Upvotes: 1