方法一【win下不乱码】
<?php
header('Content-type: text/html; charset=utf-8');
header("Content-type:application/vnd.ms-excel;charset=UTF-8");
header("Content-Disposition:filename=test.xls"); //输出的表格名称
echo "ID\t";echo "奖品\t";echo "中奖时间\t\n";
//这是表格头字段 加\T就是换格,加\T\N就是结束这一行,换行的意思
$conn = @mysql_connect("127.0.0.1","root","123456");
if (!$conn){
die("连接数据库失败:" . mysql_error());
}
mysql_select_db("test", $conn);
mysql_query("set names 'utf8'");
$result=mysql_query("SELECT a.dianhua,b.jiang,b.`jiangtime` FROM `gouwu` as a ,`zhongjiang` as b where a.openid=b.openid and b.rid<5 ORDER BY `jiangtime` DESC");
while($row=mysql_fetch_array($result)){
echo $row[0]."\t";echo $row[1]."\t";echo $row[2]."\t\n";
}
?>
方法二【Mac下不乱码】
<?php
$datemin=$_GET['datemin'];
$datemax=$_GET['datemax'];
if (($datemin<0)or ($datemax<0)){
echo "日期错误";
exit;
}
$conn = @mysql_connect("127.0.0.1","root","123456");
if (!$conn){
die("连接数据库失败:" . mysql_error());
}
$savename = date("Ymj").'顾客进出明细'; //导出excel文件名
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type;charset=utf-8");
header("Content-Disposition: attachment; filename=".$savename.".$file_ending");
header("Pragma: no-cache");
/*查询数据库*/
mysql_select_db("hyjcs_hongyujit", $conn);
mysql_query("set names 'utf8'");
$sql = "SELECT a.`id`,a.`name` as 姓名,a.`dianhua` as 电话,a.`xiaoqu` as 位置,b.`jinchu` as 进出情况,b.tiwen as 体温,b.uptime as 更新时间 FROM `dengji`a,jinchumingxi b WHERE a.openid=b.openid and b.uptime >'".$datemin." 00:00:00' and b.uptime<'".$datemax." 23:59:59' ORDER BY b.`uptime`";
$result=mysql_query($sql);
/*写入表字段名*/
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo iconv("utf-8","gbk",mysql_field_name($result,$i)) . "\t";
// 标题转码
}
echo "\n";
/*写入表数据*/
$sep = "\t";
while($row = mysql_fetch_row($result)) {
$data = "";
for($i=0; $i<mysql_num_fields($result);$i++) {
if(!isset($row[$i]))
$data .= "NULL".$sep; //处理NULL字段
elseif ($row[$i] != ""){
$datmp=iconv("utf-8", "gbk",$row[$i]);
$data .= $datmp.$sep;
}
else
$data .= "".$sep; //处理空字段
}
echo $data."\n";
}
?>
© 版权声明
文章版权归作者所有,未经允许请勿转载。