我有一个非常简单的东西,它只是输出一些CSV格式的东西,但它必须是UTF-8。我在TextEdit或TextMate或Dreamweaver中打开这个文件,它会正确地显示UTF-8字符,但如果我在Excel中打开它,它会做这种愚蠢的íÄ之类的事情。下面是我在我的文档头部得到的内容:

header("content-type:application/csv;charset=UTF-8");
header("Content-Disposition:attachment;filename=\"CHS.csv\"");

这一切似乎都达到了预期的效果,除了Excel (Mac, 2008)不想正确地导入它。Excel里没有“以UTF-8格式打开”之类的选项,所以……我有点烦了。

我似乎在任何地方都找不到任何明确的解决方案,尽管很多人都有同样的问题。我看到的最多的事情是包括BOM,但我不知道如何做到这一点。正如你所看到的,我只是回显这些数据,我没有写入任何文件。如果我需要,我可以这样做,我只是没有因为在这一点上似乎不需要这样做。任何帮助吗?

更新:我尝试将BOM作为回显包(“CCC”,0xef, 0xbb, 0xbf);这是我刚刚从一个试图检测BOM的网站上找到的。但Excel只是在导入时将这三个字符附加到第一个单元格,仍然会把特殊字符弄乱。


当前回答

**This is 100% works fine in excel for both Windows7,8,10 and also All Mac OS.**
//Fix issues in excel that are not displaying characters containing diacritics, cyrillic letters, Greek letter and currency symbols.

function generateCSVFile($filename, $headings, $data) {

    //Use tab as field separator
    $newTab  = "\t";
    $newLine  = "\n";

    $fputcsv  =  count($headings) ? '"'. implode('"'.$newTab.'"', $headings).'"'.$newLine : '';

    // Loop over the * to export
    if (! empty($data)) {
      foreach($data as $item) {
        $fputcsv .= '"'. implode('"'.$newTab.'"', $item).'"'.$newLine;
      }
    }

    //Convert CSV to UTF-16
    $encoded_csv = mb_convert_encoding($fputcsv, 'UTF-16LE', 'UTF-8');

    // Output CSV-specific headers
    header('Set-Cookie: fileDownload=true; path=/'); //This cookie is needed in order to trigger the success window.
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$filename.csv\";" );
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: '. strlen($encoded_csv));
    echo chr(255) . chr(254) . $encoded_csv; //php array convert to csv/excel
    exit;
}

其他回答

当我有一个导入数据的Excel VBA例程时,我也遇到了同样的问题。由于CSV是纯文本格式,我通过编程方式在简单的文件编辑器(如写字板)中打开数据,并将其重新保存为unicode文本,或者从那里将其复制到剪贴板并粘贴到Excel中来解决这个问题。如果excel不能自动地将CSV解析为单元格,可以使用内置的“文本到列”功能轻松地进行补救。

**This is 100% works fine in excel for both Windows7,8,10 and also All Mac OS.**
//Fix issues in excel that are not displaying characters containing diacritics, cyrillic letters, Greek letter and currency symbols.

function generateCSVFile($filename, $headings, $data) {

    //Use tab as field separator
    $newTab  = "\t";
    $newLine  = "\n";

    $fputcsv  =  count($headings) ? '"'. implode('"'.$newTab.'"', $headings).'"'.$newLine : '';

    // Loop over the * to export
    if (! empty($data)) {
      foreach($data as $item) {
        $fputcsv .= '"'. implode('"'.$newTab.'"', $item).'"'.$newLine;
      }
    }

    //Convert CSV to UTF-16
    $encoded_csv = mb_convert_encoding($fputcsv, 'UTF-16LE', 'UTF-8');

    // Output CSV-specific headers
    header('Set-Cookie: fileDownload=true; path=/'); //This cookie is needed in order to trigger the success window.
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$filename.csv\";" );
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: '. strlen($encoded_csv));
    echo chr(255) . chr(254) . $encoded_csv; //php array convert to csv/excel
    exit;
}

对我来说,上面的解决方案都不起作用。下面是我解决这个问题的方法: 在PHP代码中使用这个函数修改值:

$value = utf8_encode($value);

这将在excel表格中正确地输出值。

我也有同样(或类似)的问题。

在我的情况下,如果我添加一个BOM输出,它工作:

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=Customers_Export.csv');
echo "\xEF\xBB\xBF"; // UTF-8 BOM

我相信这是一个非常丑陋的黑客,但它对我有效,至少对Excel 2007 Windows有效。不确定在Mac上能不能用。

当你把它保存为txt文件,他们用逗号作为分隔符在excel中打开它时,问题还会发生吗?

问题可能根本不是编码,可能只是文件不是一个完美的CSV根据excel标准。