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


当前回答

office 365的工作解决方案

保存在UTF-16(无LE, BE) 使用分离器\t

PHP代码

$header = ['číslo', 'vytvořeno', 'ěščřžýáíé'];
$fileName = 'excel365.csv';
$fp = fopen($fileName, 'w');
fputcsv($fp, $header, "\t");
fclose($fp);

$handle = fopen($fileName, "r");
$contents = fread($handle, filesize($fileName));
$contents = iconv('UTF-8', 'UTF-16', $contents);
fclose($handle);

$handle = fopen($fileName, "w");
fwrite($handle, $contents);
fclose($handle);

其他回答

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

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

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

只是为了帮助有兴趣在Excel上打开文件实现这个线程的用户。

我使用了下面的向导,它对我来说工作得很好,导入了一个UTF-8文件。 不是透明的,但如果您已经有了该文件,则非常有用。

Open Microsoft Excel 2007. Click on the Data menu bar option. Click on the From Text icon. Navigate to the location of the file that you want to import. Click on the filename and then click on the Import button. The Text Import Wizard - Step 1 or 3 window will now appear on the screen. Choose the file type that best describes your data - Delimited or Fixed Width. Choose 65001: Unicode (UTF-8) from the drop-down list that appears next to File origin. Click on the Next button to display the Text Import Wizard - Step 2 or 3 window. Place a checkmark next to the delimiter that was used in the file you wish to import into Microsoft Excel 2007. The Data preview window will show you how your data will appear based on the delimiter that you chose. Click on the Next button to display the Text Import Wizard - Step 3 of 3. Choose the appropriate data format for each column of data that you want to import. You also have the option to not import one or more columns of data if you want. Click on the Finish button to finish importing your data into Microsoft Excel 2007.

来源:https://www.itg.ias.edu/content/how-import-csv-file-uses-utf-8-character-encoding-0

正如我在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中打开它。

只是分享一个全面的功能,可能会使您的生活更容易与CSV文件....请注意与此主题相关的最后一个函数参数

function array2csv($data, $file = '', $download = true, $mode = 'w+', $delimiter = ',', $enclosure = '"', $escape_char = "\\", $addUnicodeBom = false)
{
    $return = false;

    if ($file == '') {
        $f = fopen('php://memory', 'r+');
    } else {
        $f = fopen($file, $mode);
    }

    if ($addUnicodeBom) {
        $utf8_with_bom = chr(239) . chr(187) . chr(191);
        fwrite($f, $utf8_with_bom);
    }


    foreach ($data as $line => $item) {

        fputcsv($f, $item, $delimiter, $enclosure, $escape_char);
    }

    rewind($f);

    if ($download == true) {
        $return = stream_get_contents($f);
    } else {
        $return = true;
    }

    return $return;
}

找到了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。