简体   繁体   English

UTF-8 CSV编码

[英]UTF-8 csv encoding

I have a page that functions as a cognitive test for a study i'm doing. 我有一个页面用作我正在进行的研究的认知测试。

The JS file outputs a CSV file with a detailed results of the tests. JS文件输出带有测试详细结果的CSV文件。

Some of the text is in Hebrew and the CSV shows gibberish in Excel. 某些文本在希伯来语中,而CSV在Excel中显示乱码。

I tried the following method: 我尝试了以下方法:

var csvContent = "data:text/csv;charset=utf-8,";

But i get the same result: 但我得到相同的结果:

׳©׳—׳•׳¨    ׳¡׳’׳•׳ gray
׳©׳—׳•׳¨    ׳™׳¨׳•׳§    yellow
׳©׳—׳•׳¨    ׳׳“׳•׳  pink
׳׳₪׳•׳¨ ׳׳₪׳•׳¨ purple
׳™׳¨׳•׳§    ׳›׳×׳•׳ #FE642E
׳™׳¨׳•׳§    ׳¦׳”׳•׳‘    red
׳׳₪׳•׳¨ ׳©׳—׳•׳¨    pink
׳׳₪׳•׳¨ ׳›׳×׳•׳ gray
׳™׳¨׳•׳§    ׳¦׳”׳•׳‘    purple
׳׳“׳•׳  ׳׳₪׳•׳¨ pink

What am i doing wrong? 我究竟做错了什么?

Excel does not automatically recognise the encoding of UTF-8 documents. Excel不会自动识别UTF-8文档的编码。 To achieve this, you need to add a UTF-8 BOM ("\bf") to the very start of the file. 为此,您需要在文件的开头添加一个UTF-8 BOM(“ \\ uefbbbf”)。

You can also validate the encoding of the csv file with a Notepad++ before opening in Excel. 您还可以在Excel中打开之前使用Notepad ++验证csv文件的编码。 Without the BOM, Notepad++ should mark the type as "UTF-8 w/o BOM". 没有BOM表,Notepad ++应该将类型标记为“ UTF-8 w / o BOM”。 With the BOM, it will show "UTF-8". 使用BOM,它将显示“ UTF-8”。

As Alastair pointed out, you will want to have a BOM at the beginning of the file if you want excel to behave correctly. 正如Alastair指出的那样,如果您希望excel正确运行,则需要在文件的开头有一个BOM。 But I believe it should be specified differently. 但我认为应该以不同的方式指定。 Here is a complete working example of how to download (already encoded) a csv file that was built in the browser: 这是一个完整的工作示例,说明如何下载(已编码)浏览器中内置的csv文件:

// not needed with firefox, chrome, ie11:
// window.URL = window.URL || window.webkitURL;

var data = "a,column b,c\nНикола Тесла,234,365";

// add UTF-8 BOM to beginning so excel doesn't get confused.
// *THIS IS THE KEY*
var BOM = String.fromCharCode(0xFEFF);
data = BOM + data;

var btn = document.createElement("button");
btn.appendChild(document.createTextNode("Click Me!"));
btn.onclick = function() {
  var blob = new Blob([data], {type:  "text/csv;charset=UTF-8"});
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {

    // ie
    var success = window.navigator.msSaveOrOpenBlob(blob, "Name of File.csv");
    if (!success) {
      alert("Failed");
    }
  } else {

    // not ie
    var a = document.createElement("a");
    a.href = window.URL.createObjectURL(blob);
    a.download = "Name of File.csv";
    document.body.appendChild(a);
    a.click();

    // is there a problem with removing this from the DOM already?
    a.parentNode.removeChild(a);
  }
};
document.body.appendChild(btn);

The above works in current Firefox, Chrome, and IE11 -- if you open with excel, you will see Nicola Tesla's name in Serbian Cyrillic. 上面的代码可以在当前的Firefox,Chrome和IE11中使用-如果使用excel打开,您会在Serbian Cyrillic中看到Nicola Tesla的名字。

The only thing to do is add the "\" at the beginning of your csv string: 唯一要做的是在csv字符串的开头添加“ \\ ufeff”

var csv = "\ufeff"+CSV;

Same answer from here: same answer 来自此处的相同答案相同答案

I found the solution from here: similar problem and solution 我从这里找到了解决方案: 类似的问题和解决方案

I put them here just in case you are searching for a solution. 我把它们放在这里,以防万一您正在寻找解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM