Outputting UTF-8 CSV for Proper Excel Compatibility
To address the issue of Excel not properly recognizing UTF-8 characters in CSV files generated in PHP, a practical solution involves adding a Byte Order Mark (BOM) to the output.
header('Content-type: text/csv; charset=UTF-8'); header('Content-Disposition: attachment; filename=Customers_Export.csv'); echo "\xEF\xBB\xBF"; // UTF-8 BOM
The BOM is a three-byte sequence that indicates the byte order of the Unicode characters in the file. Including it ensures that Excel can interpret the file correctly as a UTF-8 encoded CSV. This method has been shown to be effective for Excel 2007 on Windows. While it may not work universally on Macs, it is a widely accepted and successful approach.
The above is the detailed content of How Can I Ensure My PHP-Generated CSV Files Are UTF-8 Compatible with Excel?. For more information, please follow other related articles on the PHP Chinese website!