I'm developing a part of an application that's responsible for exporting some data into CSV files. The application always uses UTF-8 because of its multilingual nature at all levels. But opening such CSV files (containing e.g. diacritics, cyrillic letters, Greek letters) in Excel does not achieve the expected results showing something like Г„/Г¤, Г–/Г¶. And I don't know how to force Excel understand that the open CSV file is encoded in UTF-8. I also tried specifying UTF-8 BOM EF BB BF, but Excel ignores that.

有什么解决办法吗?

附注:哪些工具可能像Excel一样?


更新

I have to say that I've confused the community with the formulation of the question. When I was asking this question, I asked for a way of opening a UTF-8 CSV file in Excel without any problems for a user, in a fluent and transparent way. However, I used a wrong formulation asking for doing it automatically. That is very confusing and it clashes with VBA macro automation. There are two answers for this questions that I appreciate the most: the very first answer by Alex https://stackoverflow.com/a/6002338/166589, and I've accepted this answer; and the second one by Mark https://stackoverflow.com/a/6488070/166589 that have appeared a little later. From the usability point of view, Excel seemed to have lack of a good user-friendly UTF-8 CSV support, so I consider both answers are correct, and I have accepted Alex's answer first because it really stated that Excel was not able to do that transparently. That is what I confused with automatically here. Mark's answer promotes a more complicated way for more advanced users to achieve the expected result. Both answers are great, but Alex's one fits my not clearly specified question a little better.


更新2

在最后一次编辑5个月后,我注意到Alex的答案不知为何消失了。我真的希望这不是一个技术问题,我希望现在不再有关于哪个答案更好的讨论。所以我认为马克的答案是最好的。


当前回答

php生成的CSV文件也有同样的问题。 当分隔符在内容开头通过“sep=,\n”定义时(当然是在BOM之后),Excel会忽略BOM。

因此,在内容的开头添加一个BOM ("\xEF\xBB\xBF"),并通过fputcsv($fh, $data_array, ";")设置分号作为分隔符;很管用。

其他回答

我尝试了我能在这个帖子上找到的一切,类似的,没有什么是完全有效的。然而,导入到谷歌表和简单地下载为csv工作就像一个魅力。如果你到了我的挫败点,可以试试。

这是我的工作解决方案:

vbFILEOPEN = "your_utf8_file.csv"
Workbooks.OpenText Filename:=vbFILEOPEN, DataType:=xlDelimited, Semicolon:=True, Local:=True, Origin:=65001

密钥是Origin:=65001

Alex是正确的,但是由于你必须导出到csv,你可以在打开csv文件时给用户这样的建议:

另存为csv格式 打开Excel 使用“data”导入数据——>导入外部数据——>导入数据 选择文件类型“csv”并浏览到您的文件 在导入向导中将File_Origin更改为“65001 UTF”(或选择正确的语言字符标识符) 将分隔符更改为逗号 选择要导入的位置并完成

这样特殊字符才能正确显示。

我正在从一个简单的c#应用程序生成csv文件,也遇到了同样的问题。我的解决方案是确保文件是用UTF8编码编写的,如下所示:

// Use UTF8 encoding so that Excel is ok with accents and such.
using (StreamWriter writer = new StreamWriter(path, false, Encoding.UTF8))
{
    SaveCSV(writer);
}

我最初有以下代码,其中口音在notepad++中看起来很好,但在Excel中被破坏:

using (StreamWriter writer = new StreamWriter(path))
{
    SaveCSV(writer);
}

你的里程可能会有所不同——我使用的是。net 4和Office 365中的Excel。

首先将Excel电子表格保存为Unicode文本。使用ie浏览器打开TXT文件,点击“另存为”TXT编码-选择合适的编码,例如Win Cyrillic 1251