I am looking for a solution to create CSV file from data from array and download it by user. Of course after button will be pushed. Which option need minimal effort and is the best for angular5?
I found one:
npm install file-saver --save
You don't need Angular to do this, just plain vanilla javascript:
// create the csv
const headers = ['header1', 'header2', ...];
let csvContent = 'data:text/csv;charset=utf-8,%EF%BB%BF';
csvContent += headers.join(';') + '\n';
for (const d of data) {
const row = [d.cell1, d.cell2].join(';');
csvContent += row + '\n';
}
// do the download stuff
const encodedUri = csvContent;
const link = document.createElement('a');
link.setAttribute('target', '_blank');
link.setAttribute('href', encodedUri);
link.setAttribute('download', `your_filename.csv`);
document.body.appendChild(link);
link.click();
link.remove();