我的程序导出到Execl全是乱码
<?php
require_once '../data/function.php';
header("Content-Type:text/html;charset=gbk");
if(isset($_POST['time1'])&&($_POST['time1'])!="")
{
$time1=$_POST['time1'];
$time3=strtotime($time1." 00:00:00");
}else{
$time1="2010-12-1";
$time3=strtotime($time1." 00:00:00");
}
if(isset($_POST['time2'])&&($_POST['time2'])!="")
{
$time2=$_POST['time2'];
$time4=strtotime($time2." 23:59:59");
}else{
$time2=date("Y-m-d",time()+8*60*60);
$time4=strtotime($time2." 23:59:59");
}
if(isset($_POST['s3']))
{
$s3=$_POST['s3'];

header("Content-Type: application/vnd.ms-excel;charset=gbk");
header("Content-Disposition: attachment; filename=report.xls");
header("Pragma: no-cache");
header("Expires: 0");
$crlf = "\n";
$sep = "\t";
//取回数据
$aquery=get("select realname,username from pw_members where username!='admin' and realname like '".$s3."%' and realname != ''");
while($rs=mysql_fetch_array($aquery))
{
$realname=$rs['realname'];
$fenshu=get1("select sum(fenshu) from pw_shenhe where author like '".$rs['username']."%' and shenhetime between '".$time3."' and '".$time4."'",'sum(fenshu)');if($fenshu==""){$fenshu='0';}
$ycxtjf=checklogin("select * from pw_shenhe where author='".$rs['username']."' and type='分' and shenhetime between '".$time3."' and '".$time4."'");
//echo $realname.$sep;
echo $realname."\t";  
echo $fenshu.$sep;
echo $ycxtjf.$sep;
echo $crlf;
}
}
?>
功能函数里面也做了 set names gbk的 但是程序导出后中文的部分($realname)就全是乱码 请高手帮忙看看!

解决方案 »

  1.   


    必须统一编码:1. 执行数据操作前执行mysql_query("set names utf8");2. 网页<head>标签里加入header("Content-type: text/html; charset=utf-8");  3. 确保文件本身就是UTF-8编码;如果是gb2312或者gbk编码,替换即可
      

  2.   

    PHP导出Excel乱码原因:网页编码与Excel编码不一致。解决办法:既然编码不一致,那让其一致了就OK了。定义Excel的字符集:
    header("Content-Type: application/vnd.ms-excel; charset=UTF-8"),看到charset=UTF-8了吧,让其与您网页编码一致即可解决Excel内数据乱码的问题了,这个比较简单!