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

后端 未结 30 2472
半阙折子戏
半阙折子戏 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:09

    Converting already utf-8 encoded text by using mb_convert_encoding is not needed. Just add three characters in front of the original content:

    $newContent = chr(239) . chr(187) . chr(191) . $originalContent
    

    For me this resolved the problem of special characters in csv files.

    0 讨论(0)
  • 2020-11-22 06:09

    You may append the 3 bytes to the file before exporting, it works for me . Before doing that system only work in Windows and HP -UX but failed in Linux.

    FileOutputStream fStream = new FileOutputStream( f );
    final byte[] bom = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
    OutputStreamWriter writer = new OutputStreamWriter( fStream, "UTF8" );
    fStream.write( bom );
    

    Have a UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file. Otherwise Excel will interpret the data according to your locale's default encoding (e.g. cp1252) instead of utf-8

    Generating CSV file for Excel, how to have a newline inside a value

    0 讨论(0)
  • 2020-11-22 06:10

    EASY solution for Mac Excel 2008: I struggled with this soo many times, but here was my easy fix: Open the .csv file in Textwrangler which should open your UTF-8 chars correctly. Now in the bottom status bar change the file format from "Unicode (UTF-8)" to "Western (ISO Latin 1)" and save the file. Now go to your Mac Excel 2008 and select File > Import > Select csv > Find your file > in File origin select "Windows (ANSI)" and voila the UTF-8 chars are showing correctly. At least it does for me...

    0 讨论(0)
  • 2020-11-22 06:11

    Here is how I did it (that's to prompt browser to download the csv file):

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=file.csv');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    echo "\xEF\xBB\xBF"; // UTF-8 BOM
    echo $csv_file_content;
    exit();
    

    The only thing it fixed UTF8 encoding problem in CSV preview when you hit space bar on Mac.. but not in Excel Mac 2008... don't know why

    0 讨论(0)
  • 2020-11-22 06:11

    This works fine in excel for both Windows and also Mac OS.

    Fix issues in excel that are not displaying characters containing diacritics, cyrillic letters, greek letter and currency symbols.

    function writeCSV($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;
    }
    
    0 讨论(0)
  • 2020-11-22 06:12

    you can convert your CSV String with iconv. for example:

    $csvString = "Möckmühl;in Möckmühl ist die Hölle los\n";
    file_put_contents('path/newTest.csv',iconv("UTF-8", "ISO-8859-1//TRANSLIT",$csvString) );
    
    0 讨论(0)
提交回复
热议问题