简体   繁体   English

使用PHP将带有UTF8的CSV导出到Excel

[英]Exporting CSV with UTF8 to Excel using PHP

my client asked me to build an export system that export the whole SQL database into csv file that works on excel. 我的客户要求我建立一个导出系统,将整个SQL数据库导出到可在excel上使用的csv文件。 I found PHPexcel and it's great, but I thought I can do stuff way more easier and faster using my own functions. 我发现PHPexcel很棒,但是我认为我可以使用自己的函数更轻松,更快速地完成工作。

After struggling with Excel encoding, I finally succeeded to export the CSV that will work on Excel using the following code: 在使用Excel编码后,我终于成功使用以下代码导出了将在Excel上运行的CSV:

   <?php

    $data = showData("price"); //Function that loads all the SQL database into an array. 

    function array2csv(array &$array)
    {
       if (count($array) == 0) {
         return null;
       }
       ob_start();
       $df = fopen("php://output", 'w');
       fputcsv($df, array_keys(reset($array)));
       foreach ($array as $row) {
          fputcsv($df, $row);
       }
       fclose($df);
       return ob_get_clean();
    }

    function download_send_headers($filename) {
        // disable caching
        $now = gmdate("D, d M Y H:i:s");
        header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
        header("Last-Modified: {$now} GMT");
        header ( 'HTTP/1.1 200 OK' );
        header ( 'Date: ' . date ( 'D M j G:i:s T Y' ) );
        header ( 'Content-Type: application/vnd.ms-excel') ;
        header ( 'Content-Disposition: attachment;filename=export.csv' );
    }

    download_send_headers("export.csv");
    $final = array2csv($data);
    print chr(255) . chr(254) . mb_convert_encoding($final, 'UTF-16LE', 'UTF-8');
    die();

    ?>

The problem is that when I try to open the file in Excel there are no columns. 问题是,当我尝试在Excel中打开文件时,没有列。 Each row is a large column that contains the whole data of the specific row, separated by a comma. 每行都是一个大列,其中包含特定行的全部数据,并以逗号分隔。 I figured out that I would need to replace those commas with something that Excel can read as a "new column". 我发现我需要将那些逗号替换为Excel可以读取为“新列”的内容。 But I still need to keep my CSV to work as it is. 但是我仍然需要保持CSV不变。

I searched SO and Google with no luck whatsoever finding a solution that will keep my CSV intact and yet split the data into columns Excel. 我在搜索SO和Google时一无所获,没有找到可以保持CSV完整但又将数据分成Excel列的解决方案。 If there is no way to do both, I think the more important thing to my client is that the Excel version will work as it should (each row separated into columns). 如果没有办法做到这两者,我认为对我的客户来说更重要的是Excel版本将按预期工作(每行分成几列)。

This is a picture of how it looks on CSV (using numbers on Mac) 这是CSV外观的图片(在Mac上使用数字) CSV文件

And this is a picture of how it looks on Excel 2007 on Windows 这是Windows在Excel 2007上的外观图 Excel文件

It's all in the fputcsv() function: 全部在fputcsv()函数中:

http://php.net/manual/en/function.fputcsv.php http://php.net/manual/zh/function.fputcsv.php

Try to choose the correct $delimiter , $enclosure and $escape_char . 尝试选择正确的$delimiter$enclosure$escape_char

The default character that is used as the field-separator in excel is set by the locale settings. 在excel中用作字段分隔符的默认字符由区域设置设置。 That means: Importing CVS files can be language dependent. 这意味着:导入CVS文件可能取决于语言。 Export to CSV from excel to see what it does on your system, and check that your client system has the same settings. 从excel导出为CSV,以查看其在系统上的作用,并检查您的客户端系统具有相同的设置。

It's better to export to XML instead of CSV, because that will circumvent this problem: 最好导出为XML而不是CSV,因为这可以避免此问题:

http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM