php export to excel using utf-8 for french characters
Posted: Mon May 16, 2011 4:36 pm
hi everyone.
i'm using the following script to export data from mysql to an excel spreadsheet. the export does function, however when the data is exported and the excel file is opened, weird characters like è é ê ë are displayed instead of french characters.
the data stored in the mysql database is utf-8.
i have tried:
header("Content-type: text/html; charset: utf-8");
and
header("Content-type:application/vnd.ms-excel;charset:UTF-8");
but neither header make any difference to the output. i still receive weird characters. any ideas?
i'm using the following script to export data from mysql to an excel spreadsheet. the export does function, however when the data is exported and the excel file is opened, weird characters like è é ê ë are displayed instead of french characters.
the data stored in the mysql database is utf-8.
i have tried:
header("Content-type: text/html; charset: utf-8");
and
header("Content-type:application/vnd.ms-excel;charset:UTF-8");
but neither header make any difference to the output. i still receive weird characters. any ideas?
Code: Select all
<?PHP
include('db.php');
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
// file name for download
$filename = "WebsiteData_" . date('Y-m-d') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-type: text/html; charset: utf-8");
$flag = false;
$result = mysql_query("SELECT CompanyName, CONCAT(NameLast_1, ', ', NameFirst_1) As Name, DateRecorded, DateClosed
FROM records
GROUP BY CompanyName
ORDER BY CompanyName") or die('Query failed!');
while(false !== ($row = mysql_fetch_assoc($result))) {
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\n";
}
?>