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的答案不知为何消失了。我真的希望这不是一个技术问题,我希望现在不再有关于哪个答案更好的讨论。所以我认为马克的答案是最好的。


当前回答

现在是2022年3月,似乎我们不能同时使用BOM和sep=…线。 添加sep=\t或类似的,使Excel忽略BOM。

使用分号似乎是Excel的默认理解,在这种情况下,我们可以跳过sep=…这样就行了。

这是微软365与Excel版本2110构建14527.20276。

其他回答

找到了ASP的解决方案。使用POM下载UTF8格式的CSV文件:

byte[] csvBytes = Encoding.Default.GetBytes(csvString);
UTF8Encoding utf8 = new UTF8Encoding(true);
byte[] bom = utf8.GetPreamble();
var result = bom.Concat(csvBytes).ToArray();
return new FileContentResult(result, MediaTypeHeaderValue.Parse("text/csv; charset=utf-8"));

Excel是识别下载的CSV文件而不是UTF8。

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

老问题了,但最简单的解决方法是:

在记事本中打开CSV 另存为->选择正确的编码 打开新文件

您可以转换。csv文件到UTF-8与BOM通过notepad++:

在notepad++中打开文件。 进入“编码→转换为UTF-8-BOM”菜单。 进入菜单文件→保存。 关闭记事本+ +。 在Excel中打开文件。

在Microsoft Excel 2013 (15.0.5093.1000) MSO(15.0.5101.1000) 64位中工作,来自Microsoft Office Professional Plus 2013在Windows 8.1上,非unicode程序的区域设置为“德语(德国)”。

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

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