JavaScript:如何下载包含具有UTF-8编码的法语文本的CSV文件 [英] JavaScript: how to Download CSV file containing French text with UTF-8 encoding
问题描述
我正在尝试使用JavaScript下载CVS文件.该CSV包含法语文本. 我正在使用Blob创建文件
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);
但是当我打开csv文件时,由于ANSI格式的法语字符被转换成一些未知字符,因此它以ANSII格式显示内容.
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.
但是,如果我打开与文本相同的文件,则编码就可以了(UTF-8).
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
如何直接使用UTF-8编码打开下载CSV文件"? 我不希望用户更改Excel中的任何内容.我想要一些javascript格式,以便excel可以识别我的所有字符,而不管其编码如何.有可能做点什么吗?
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?
推荐答案
我遇到了同样的问题,并且找到了以下解决方案:
I met the same issue, and I found the following solution:
唯一要做的就是在csv字符串的开头添加"\ ufeff" :
The only thing to do is add the "\ufeff" at the beginning of your csv string:
var csv = "\ufeff"+CSV;
这篇关于JavaScript:如何下载包含具有UTF-8编码的法语文本的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!