0

I am creating a CSV file from some data in javascript. My function looks like:

function exportCSV() {
  let csvContent = "data:text/csv;charset=utf-8,";
  let header = "color, make, model,";
  csvContent = csvContent + "\r\n" + header;

  csvData.forEach(function(rowArray) {
    let row = rowArray.join(",");
    csvContent += row + "\r\n";
});

var encodedUri = encodeURI(csvContent);
window.open(encodedUri);

}

As it is, the CSV that I get from this function has the headers, but then the data immediately follows the last header name and does not have a carriage return. Is there a character I am missing, or perhaps doing these things in the wrong order?

LolEb
  • 9
  • 2
  • 2
    csvContent = csvContent + "\r\n" + header; should be csvContent = csvContent + "\r\n" + header + "\r\n"; I would say. – Ole EH Dufour Oct 28 '20 at 19:18
  • Ah, of course. I gave your suggestion a try, but it curiously just puts all of the contents on the second row. It did separate the last header from the data though, which is a plus. Thank you for the help regardless. – LolEb Oct 28 '20 at 19:44
  • What does the csvData look like? Is it an array of arrays like in this exemple https://stackoverflow.com/a/14966131/4180382 ? – Ole EH Dufour Oct 28 '20 at 20:55

0 Answers0