我爱学习网首页 | 设为首页 | 收藏本站 | 网站地图 您好,欢迎光临我爱学习网
我爱学习网

当前位置:首页 > 编程开发 > php >

php导出数据到excel表格

时间:2014-05-06 20:04 来源:我爱学习网 | 作者:我爱学习网 | 本文已影响
php开发中,常常需要将mysql数据库中表的记录导出到excel表格中,以方便浏览等。

php导出数据到excel表格程序代码如下:

$filename=date("Y-m-d H:i:s").".xls";//先定义一个excel文件
header("Content-Type: application/vnd.ms-execl"); 
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=$filename"); 
header("Pragma: no-cache"); 
header("Expires: 0");

//我们先在excel输出表头,当然这不是必须的
echo iconv("utf-8", "gb2312", "访问时间")."\t";
echo iconv("utf-8", "gb2312", "IP")."\t";
echo iconv("utf-8", "gb2312", "来源地区")."\t";
echo iconv("utf-8", "gb2312", "来路")."\t";
echo iconv("utf-8", "gb2312", "关键词")."\t";
echo iconv("utf-8", "gb2312", "受访页面")."\n";

//输出表中记录

$query=mysql_query("SELECT * FROM `logs` order by `id` desc");
if($query){
	while($row=mysql_fetch_array($query)){
                echo iconv("utf-8", "gb2312", $fw_time)."\t";
		echo iconv("utf-8", "gb2312", $row['ip'])."\t";
		echo iconv("utf-8", "gb2312", $row['ip_city'])."\t";
		echo iconv("utf-8", "gb2312", $row['lailu'])."\t";
		echo iconv("utf-8", "gb2312", $row['word'])."\t";
		echo iconv("utf-8", "gb2312", $row['page'])."\n";
	}
}

(责任编辑:我爱学习网)


分享到: 更多