Blog de Nathan Story

Force Excel to Interpret a CSV File as UTF-8

| Comments

The trick

By default, Microsoft Excel will assume a CSV file is encoded in Windows-1252. You can, however, trick Excel into using UTF-8 (AKA the one true character encoding) by prepending to the file with the UTF-8 BOM 0xEF 0xBB 0xBF.

A PHP example

Below is a self-contained PHP script that demonstrates this:

<?php
// if you are pasting this into your text editor, make sure it's
// set to use UTF-8 character encoding
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="example.csv"');
echo "\xEF\xBB\xBF";
echo "language,greeting\n";
echo "English,Hello\n";
echo "Japanese,こんにちは\n";
echo "Chinese,你好\n";

Comments