最近做了一个网站,后台导出excel表格用wps表格打开正常,但是用微软excel表格打开中文乱码。做了以下各种调试:
1.$title = iconv(“utf-8″, “gb2312″, $title); 内容强制转换成gb2312
2.在表格导出操作前加上这段代码,ob_end_clean();//清除缓冲区,避免乱码
ob_end_clean();//解决乱码核心 就在此处添加此函数 header('Content-Type: application/vnd.ms-excel');
3.header(“Content-Type: application/vnd.ms-excel; charset=utf-8″);将charset改成utf-8,gbk,iso-8859-1
4.更改文本编码
5.chr(0xEF).chr(0xBB).chr(0xBF).$html 使用bom
各种调试并无卵用。。。
之后使用HTMLExcel解决问题。代码如下:
$table_csv_output=’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″>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html>
<head>
<meta http-equiv=”Content-type” content=”text/html;charset=UTF-8″ />
<style id=”Classeur1_16681_Styles”></style>
</head>
<body>
<div id=”Classeur1_16681″ align=center x:publishsource=”Excel”>
<table x:str border=0 cellpadding=0 cellspacing=0 width=100% style=”border-collapse: collapse”>
<tr><td nowrap>公司</td><td nowrap>联系方式</td></tr>
<tr><td nowrap>万方</td><td nowrap>全国免费电话: 400 9315019</td></tr>
</table>
</div>
</body>
</html>’;
header(“Content-Type: application/vnd.ms-excel; charset=utf-8″);
header(“Pragma: public”);
header(“Expires: 0″);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0″);
header(“Content-Type: application/force-download”);
header(“Content-Type: application/octet-stream”);
header(“Content-Type: application/download”);
header(“Content-Disposition: attachment;filename=customers_”. date(“Ymd”) . “.xls “);
header(“Content-Transfer-Encoding: binary “);
echo $table_csv_output;
这样把header加上,之后直接echo,又不需要iconv转码,只要设置好HTML里的Content-type(这里用的是UTF-8),用微软表格打开就不乱码了,当然wps打开也不乱码