JavaScript: how to Download CSV file containing French text with UTF-8 encoding

对着背影说爱祢 提交于 2019-12-07 16:10:06

问题


I am trying to download CVS file using JavaScript. That CSV contains text in French language. I am creating file using blob

    var blob = new Blob([ csv ], {
                type : 'text/csv;charset=utf-8'
            });
    var csvUrl = window.URL.createObjectURL(blob);

But when i open the csv file it shows the content in ANSII format due to which French characters are converted to some unknown characters.

But if i open the same file as text, the encoding is ok (UTF-8).

Example: Text I am trying to download "Opération"
Text Visible in Excel (.csv) ==> Opération   <== With unknown characters
Text if opened in Notpad++ ==> Opération   <== Correct UTF-8

How can i open Download CSV file directly with UTF-8 encoding? I don't want user to change anything in excel. I want some javascript format so that excel could recognize all my characters irrespective of its encoding. Is it possible to do something?


回答1:


I met the same issue, and I found the following solution:

similar problem and solution

The only thing to do is add the "\ufeff" at the beginning of your csv string:

var csv = "\ufeff"+CSV;


来源:https://stackoverflow.com/questions/43463923/javascript-how-to-download-csv-file-containing-french-text-with-utf-8-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!