Javascript export CSV encoding utf-8 issue Javascript export CSV encoding utf-8 issue javascript javascript

Javascript export CSV encoding utf-8 issue


This depends on what program is opening the example.csv file. Using a text editor, the encoding will be UTF-8 and the characters will not be malformed. But using Excel the default encoding for CSV is ANSI and not UTF-8. So without forcing Excel using not ANSI but UTF-8 as the encoding, the characters will be malformed.

Excel can be forced using UTF-8 for CSV with putting a BOM (Byte Order Mark) as first characters in the file. The default BOM for UTF-8 is the byte sequence 0xEF,0xBB,0xBF. So one could think simply putting "\xEF\xBB\xBF" as first bytes to the string will be the solution. But surely that would be too simple, wouldn't it? ;-) The problem with this is how to force JavaScript to not taking those bytes as characters. The "solution" is using a "universal BOM" "\uFEFF" as mentioned in Special Characters (JavaScript).

Example:

var csvString = 'ı,ü,ü,ğ,ş,#Hashtag,ä,ö';var universalBOM = "\uFEFF";var a = window.document.createElement('a');a.setAttribute('href', 'data:text/csv; charset=utf-8,' + encodeURIComponent(universalBOM+csvString));a.setAttribute('download', 'example.csv');window.document.body.appendChild(a);a.click();