Trouble Exporting UTF-8 CSV for Excel
Despite proper headers for UTF-8 encoding, Excel stubbornly displays special characters incorrectly when importing a CSV generated with PHP. This frustrating issue has perplexed many users, including the author of the original post.
The common suggestion of including the BOM (Byte Order Mark) has proved elusive for some, as adding it results in Excel appending the BOM to the first cell. However, a solution emerged from a user who found success by explicitly adding the BOM to the output using the following code:
header('Content-type: text/csv; charset=UTF-8'); header('Content-Disposition: attachment; filename=Customers_Export.csv'); echo "\xEF\xBB\xBF"; // UTF-8 BOM
Although this approach is not ideal, it effectively resolved the encoding issue for Excel 2007 Windows. It is uncertain whether or not it will work on Mac OS.
The above is the detailed content of Why Does My UTF-8 CSV File Display Incorrect Special Characters in Excel, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!