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

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

    How about just outputting for Excel itself? This is an excellent class that allows you to generate XLS files server-side. I use it frequently for clients who can't "figure out" csv's and so far have never had a complaint. It also allows some extra formatting (shading, rowheights, calculations, etc) that csv won't ever do.

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

    To follow up on this:

    It appears that the problem is simply with Excel on the Mac. It's not how I'm generating the files, because even generating CSVs from Excel is breaking them. I save as CSV, and reimport, and all the characters are messed up.

    So … there doesn't appear to be a correct answer to this. Thanks for all the suggestions.

    I would say that from all I've read, @Daniel Magliola's suggestion about the BOM would probably be the best answer for some other computer. But it still doesn't solve my problem.

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

    As I investigated and I found that UTF-8 is not working well on MAC and Windows so I tried with Windows-1252 , it supports well on both of them but you must select type of encoding on ubuntu. Here is my code$valueToWrite = mb_convert_encoding($value, 'Windows-1252');

    $response->headers->set('Content-Type', $mime . '; charset=Windows-1252');
        $response->headers->set('Pragma', 'public');
        $response->headers->set('Content-Endcoding','Windows-1252');
        $response->headers->set('Cache-Control', 'maxage=1');
        $response->headers->set('Content-Disposition', $dispositionHeader);
        echo "\xEF\xBB\xBF"; // UTF-8 BOM
    
    0 讨论(0)
  • 2020-11-22 06:29

    I had this same problem when I had an Excel VBA routine that imported data. Since CSV is a plain text format, I was working around this by programatically opening the data in a simple file editor like wordpad, and re-saving it as unicode text, or copying it to the clipboard from there and pasting it to Excel. If excel doesn't automatically parse the CSV into cells, this is easily remedied using the built in "Text to Columns" feature.

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

    Does the problem still occur when you save it as a .txt file and them open that in excel with comma as a delimiter?

    The problem might not be the encoding at all, it might just be that the file isn't a perfect CSV according to excel standards.

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

    Otherwise you may:

    header("Content-type: application/x-download");
    header("Content-Transfer-Encoding: binary");
    header("Content-disposition: attachment; filename=".$fileName."");
    header("Cache-control: private");
    
    echo utf8_decode($output);
    
    0 讨论(0)
提交回复
热议问题