1

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

1 Answer 1

4

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.