Home > Backend Development > PHP Tutorial > How Can I Ensure My PHP-Generated CSV Files Are UTF-8 Compatible with Excel?

How Can I Ensure My PHP-Generated CSV Files Are UTF-8 Compatible with Excel?

Linda Hamilton
Release: 2024-12-17 08:00:28
Original
348 people have browsed it

How Can I Ensure My PHP-Generated CSV Files Are UTF-8 Compatible with Excel?

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

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!

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