php header excel and utf-8
ob_start();
echo 'Désçàui';
header("Content-开发者_JAVA技巧Type: application/vnd.ms-excel; charset=utf-8");
header("Content-type: application/x-msexcel; charset=utf-8");
header("Content-Disposition: attachment; filename=Test.xls");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
ob_end_flush();
What I'm getting in the excel file is Désçà ui
However, I do get Désçàui when I try
ob_start();
echo 'Désçàui';
header("Content-Type: text/html; charset=utf-8");
ob_end_flush();
Any help experts?
PS. The file is saved in DW with Title/Encoding Unicode(Utf-8).
Source http://www.mwasif.com/2007/5/download-data-csv-using-php/
Solution worked for me
Danish Zahur said,
October 7, 2009 @ 7:23 pm
If your contents are in UTF-8 format, then no need to convert encoding. Just start your file/output stream with UTF-8 BOM after headers.
echo pack("CCC",0xef,0xbb,0xbf);
And header should contain encoding UTF-8
header( "Content-type: application/vnd.ms-excel; charset=UTF-8" );
It will work like charm because Excel will recognize file charset with BOM bytes.
I'm not sure, but it may be that excel can not handle utf8(may depend on the version). But it can handle utf16, so try converting the charset. This works for me(in excel2002):
echo mb_convert_encoding('Désçàui','utf-16','utf-8');
I don't know how you're generating the excel file. But, if you're doing it from an HTML output, you can just add the following at the begining:
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
Regards
This UTF-8 BOM line of code made my UTF-8 chars work fine:
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename='".$file_name."'");
header("Pragma: no-cache");
header("Expires: 0");
echo "\xEF\xBB\xBF"; //UTF-8 BOM
echo $out;
Converting utf8 to html-entities worked quite nicely for me :
$str = 'utf-string ...';
if (mb_detect_encoding($str ) == 'UTF-8') {
$str = mb_convert_encoding($str , "HTML-ENTITIES", "UTF-8");
}
header('Content-type: application/x-msdownload; charset=utf-16');
header('Content-Disposition: attachment; filename=companies.xls');
header('Pragma: no-cache');
header('Expires: 0');
echo $str ;
The content-type
headers are relevant for the browser only. They have no effect on downloaded files. Once the file is saved, it is up to the application to decide how it treats the data in the file.
The example you show is not a valid Excel file in the first place. When encountering what it must think is a broken file, Excel probably switches to some default processing that assumes windows-1252
or some other single-byte character set.
You would have to give Excel a proper file to open. Alternatively, it may be possible to use the old "Output HTML but save as XLS" trick and specify a UTF-8 encoding in that HTML file.
try this
<?php
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="sample.xls"');
echo "
<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">
<html>
<head><meta http-equiv=\"Content-type\" content=\"text/html;charset=utf-8\" /></head>
<body>
";
echo "
<table>
<tr>
<th rowspan=\"2\" nowrap=\"nowrap\">เลขที่บัญชี</th>
<th rowspan=\"2\" nowrap=\"nowrap\">ชื่อ-สกุล ลูกค้า</th>
<th rowspan=\"2\" nowrap=\"nowrap\">OS/Balance</th>
<th rowspan=\"2\" nowrap=\"nowrap\">วันที่</th>
<th rowspan=\"2\" nowrap=\"nowrap\">เวลา</th>
<th rowspan=\"2\" nowrap=\"nowrap\">Action Code</th>
<th rowspan=\"2\" nowrap=\"nowrap\">Amount</th>
<th colspan=\"5\" nowrap=\"nowrap\">ผลการติดตาม</th>
</tr>
<tr>
<th nowrap=\"nowrap\">ที่อยู่บ้าน</th>
<th nowrap=\"nowrap\">เบอร์โทรบ้าน</th>
<th nowrap=\"nowrap\">เบอร์โทรมือถือ</th>
<th nowrap=\"nowrap\">ที่อยู่ที่ทำงาน</th>
<th nowrap=\"nowrap\">เบอร์โทรที่ทำงาน</th>
</tr>
<tr>
<td>acc</td>
<td>name</td>
<td>balance</td>
<td>date</td>
<td>time</td>
<td>code</td>
<td>amount</td>
<td>h-addr</td>
<td>h-tel</td>
<td>cell</td>
<td>w-addr</td>
<td>w-tel</td>
</tr>
</table>
";
echo "</body></html>";
?>
Try this:
header('Content-Transfer-Encoding: binary');
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Disposition: attachment; filename = "Export '.date("Y-m-d").'.xls"');
header('Pragma: no-cache');
//these characters will make correct encoding to excel
echo chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $out);
精彩评论