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


当前回答

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

其他回答

在php中,你只需要将$bom前置到$csv_string:

$bom = sprintf( "%c%c%c", 239, 187, 191); // EF BB BF
file_put_contents( $file_name, $bom . $csv_string );

使用MS Excel 2016, php 7.2.4进行测试

我们使用了以下方法:

转换CSV到UTF-16 LE 在文件开头插入BOM 使用制表符作为字段分隔符

是的,这是可能的。正如之前多个用户所指出的,当文件以UTF-8编码时,excel读取正确的字节顺序标记似乎存在问题。对于UTF-16,它似乎没有问题,所以它是UTF-8特有的。我为此使用的解决方案是添加BOM,两次。为此,我执行了两次下面的sed命令:

sed -I '1s/^/\xef\xbb\xbf/' *.csv

,其中通配符可以替换为任何文件名。然而,这会导致.csv文件开头的sep=发生突变。然后,.csv文件将在excel中正常打开,但在第一个单元格中有一个带有“sep=”的额外行。 "sep="也可以在源文件的.csv中删除,但是当用VBA打开文件时,应该指定分隔符:

Workbooks.Open(name, Format:=6, Delimiter:=";", Local:=True)

格式6是.csv格式。将Local设置为true,以防文件中有日期。如果Local未设置为true,日期将被美国化,这在某些情况下会破坏.csv格式。

我尝试了我能在这个帖子上找到的一切,类似的,没有什么是完全有效的。然而,导入到谷歌表和简单地下载为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程序的区域设置为“德语(德国)”。