Home >Backend Development >PHP Tutorial >Why Does My UTF-8 CSV File Display Incorrect Special Characters in Excel, and How Can I Fix It?

Why Does My UTF-8 CSV File Display Incorrect Special Characters in Excel, and How Can I Fix It?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-20 16:58:16730browse

Why Does My UTF-8 CSV File Display Incorrect Special Characters in Excel, and How Can I Fix It?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn