Export javascript csv utf-8 encoding

I need to export a javascript array to a CSV file and load it. I did this, but 'ı, ü, ö, ğ, ş' these characters look like 'Ä ± ü ö ÄŸ ÅŸ' in the CSV file. I tried many of the solutions recommended on this site, but did not work for me.

I added my code snippet, can anyone solve this problem?

var csvString = 'ı,ü,ö,ğ,ş';

var a = window.document.createElement('a');
a.setAttribute('href', 'data:text/csv; charset=utf-8,' + encodeURIComponent(csvString));
a.setAttribute('download', 'example.csv');
a.click();
Run codeHide result
+4
source share
1 answer

, example.csv. , UTF-8, . Excel CSV ANSI, UTF-8. , Excel, ANSI, UTF-8 , .

Excel UTF-8 CSV a BOM (, . , , , ?;-) , JavaScript ." "" ""\uFEFF", (JavaScript).

:

var csvString = 'ı,ü,ü,ğ,ş';
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();
Hide result

. UTF-8 /Blob.

, . , , , Windows. , , , Windows, , . CSV. , CSV , , Excel (*.xls *.xlsx).

+3

Source: https://habr.com/ru/post/1670845/


All Articles