Line breaks in javascript generated by csv for excel import
I am creating and loading a CSV from a webpage using javascript in the browser side (chrome windows)
function toCsv(arr){
return arr.reduce(function(csvString, row){
csvString += row.join(',') ;
csvString += "\r\n"; //";";//"\n";
return csvString;
}, '');
}
function flowDataCsv(){
document.location = 'data:Application/octet-stream,' + toCsv(flowDataGrid);
}
I tried to delimit lines with ";", "\ r \ n" and "\ n", but all three results in csv: s, which are superior to single line imports, not as a grid. How do I finish the lines to get excel to recognize it as a string and start inserting data on a new row?
+3
source to share
1 answer
Not sure if you still need an answer to this question. But will post the solution I found as I had the same problem.
Using your code above the only thing you are missing is to encode the csv string returned by your toCsv function. It will look like this:
function toCsv(arr){
return arr.reduce(function(csvString, row){
csvString += row.join(',') ;
csvString += "\r\n"; //";";//"\n";
return encodeURIComponent(csvString);
}, '');
}
function flowDataCsv(){
document.location = 'data:Application/octet-stream,' + toCsv(flowDataGrid);
}
Hope this helps anyone who encounters this problem again in the future!
+8
source to share