How can I output a UTF-8 CSV in PHP that Excel will read properly?

后端 未结 30 2511
半阙折子戏
半阙折子戏 2020-11-22 06:08

I\'ve got this very simple thing that just outputs some stuff in CSV format, but it\'s got to be UTF-8. I open this file in TextEdit or TextMate or Dreamweaver and it displa

30条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 06:36

    I just dealt with the same problem, and came up with two solutions.

    1. Use the PHPExcel class as suggested by bpeterson76.

      • Using this class generates the most widely compatible file, I was able to generate a file from UTF-8 encoded data that opened fine in Excel 2008 Mac, Excel 2007 Windows, and Google Docs.
      • The biggest problem with using PHPExcel is that it's slow and uses a lot of memory, which isn't an issue for reasonably sized files, but if your Excel/CSV file has hundreds or thousands of rows, this library becomes unusable.
      • Here is a PHP method that will take some TSV data and output an Excel file to the browser, note that it uses the Excel5 Writer, which means the file should be compatible with older versions of Excel, but I no longer have access to any, so I cannot test them.

        function excel_export($tsv_data, $filename) {
            $export_data = preg_split("/\n/", $tsv_data);
            foreach($export_data as &$row) {
                $row = preg_split("/\t/", $row);
            }
        
            include("includes/PHPExcel.php");
            include('includes/PHPExcel/Writer/Excel5.php');
        
            $objPHPExcel = new PHPExcel();          
        
            $objPHPExcel->setActiveSheetIndex(0);
            $sheet = $objPHPExcel->getActiveSheet();
            $row = '1';
            $col = "A";
            foreach($export_data as $row_cells) {
                if(!is_array($row_cells)) { continue; }
                    foreach($row_cells as $cell) {
                        $sheet->setCellValue($col.$row, $cell);
                        $col++;
                    }
                $row += 1;
                $col = "A";
            }
        
            $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
            header('Cache-Control: max-age=0');
            $objWriter->save('php://output');   
            exit;   
        }
        
    2. Because of the efficiency issues with PHPExcel, I also had to figure out how to generate a UTF-8 & Excel compatible CSV or TSV file.

      • The best I could come up with was a file that was compatible with Excel 2008 Mac, and Excel 2007 PC, but not Google Docs, which is good enough for my application.
      • I found the solution here, specifically, this answer, but you should also read the accepted answer as it explains the problem.
      • Here is the PHP code I used, note that I am using tsv data (tabs as delimiters instead of commas):

        header ( 'HTTP/1.1 200 OK' );
        header ( 'Date: ' . date ( 'D M j G:i:s T Y' ) );
        header ( 'Last-Modified: ' . date ( 'D M j G:i:s T Y' ) );
        header ( 'Content-Type: application/vnd.ms-excel') ;
        header ( 'Content-Disposition: attachment;filename=export.csv' );
        print chr(255) . chr(254) . mb_convert_encoding($tsv_data, 'UTF-16LE', 'UTF-8');
        exit;
        

提交回复
热议问题