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
Release: 2024-12-20 16:58:16
Original
728 people have browsed it

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template