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


当前回答

正如我在http://thinkinginsoftware.blogspot.com/2017/12/correctly-generate-csv-that-excel-can.html:上发表的

告诉负责生成CSV的软件开发人员纠正它。作为一个快速的解决方法,你可以使用gsed在字符串的开头插入UTF-8 BOM:

gsed -i '1s/^\(\xef\xbb\xbf\)\?/\xef\xbb\xbf/' file.csv

如果UTF-4 BOM不存在,该命令将插入。因此这是一个幂等命令。现在您应该能够双击该文件并在Excel中打开它。

其他回答

如果你想让它完全自动化,点击一下,或者从一个网页自动加载到Excel中,但不能生成适当的Excel文件,那么我建议考虑SYLK格式作为替代方案。好吧,它不像CSV那么简单,但它是基于文本的,非常容易实现,它支持UTF-8没有问题。

我写了一个PHP类,接收数据并输出一个SYLK文件,该文件将通过单击文件直接在Excel中打开(或者如果您将文件写入具有正确mime类型的web页面,将自动启动Excel)。你甚至可以添加格式(如粗体,以特定的方式格式化数字等),改变列的大小,或自动调整列的文本,所有的代码可能不超过100行。

通过创建一个简单的电子表格并保存为SYLK,然后用文本编辑器读取它,就可以非常容易地对SYLK进行逆向工程。第一个块是您可以识别的标头和标准数字格式(您只需在创建的每个文件中反刍它们),然后数据只是一个X/Y坐标和一个值。

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

在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进行测试

简单的vba宏用于打开utf-8文本和csv文件

Sub OpenTextFile()

   filetoopen = Application.GetOpenFilename("Text Files (*.txt;*.csv), *.txt;*.csv")
   If filetoopen = Null Or filetoopen = Empty Then Exit Sub

   Workbooks.OpenText Filename:=filetoopen, _
   Origin:=65001, DataType:=xlDelimited, Comma:=True

End Sub

原点:=65001为UTF-8。 逗号:对于按列分布的.csv文件为True

保存在个人。XLSB使它始终可用。 个性化excel工具栏添加一个宏调用按钮,并从那里打开文件。 您可以添加更多的格式到宏,如列自动拟合,对齐等。

这并不是准确地解决问题,但由于我偶然发现了这一点,上面的解决方案不适合我或有要求,我不能满足,这里是另一种方式添加BOM时,你可以访问vim:

vim -e -s +"set bomb|set encoding=utf-8|wq" filename.csv