Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Proper data formatting for CSV export

I'm currently exporting data (on the client side) to CSV, with specific utf-8 encoding

var csvContent = "data:text/csv;charset=utf-8,";
arr.forEach(function(infoArray, index){
 var dataString = infoArray.join(",");
 csvContent += index < arr.length ? dataString+ "\n" : dataString;
});
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "data.csv");
document.body.appendChild(link);
link.click(); 

The data is in arr and looks like :

[{'firstname':'John', 'surname':'Doe', 'city':'Paris'}, ... , {'firstname':'Johnny', 'surname':'Done', 'city':'Paris'}]

It works pretty well expect that when I'm importing the resulting file in Excel I have encoding error (É -> É for example), but when I open the file in Sublime Text everything looks fine.

Answer*

Draft saved
Draft discarded
Cancel
1
  • thanks for the snippet but even with the UTF-8 BOM there is still an encoding issue ("É" are now "√â"). Further investigation seems to point out that Excel on OSX is awfully bad at detecting file encoding. I tried using various tools (such as text-encoding and FileSaver.js ) or Blop objects but no luck so far. Commented Jan 16, 2017 at 12:51

lang-js

AltStyle によって変換されたページ (->オリジナル) /