日期:2015-04-16 分類:網(wǎng)絡(luò)技術(shù) 瀏覽:12380 來源:邦明科技
PHP從數(shù)據(jù)庫(kù)導(dǎo)出excel表格的最簡(jiǎn)單代碼和原理
代碼如下:
<?php //數(shù)據(jù)庫(kù)連接文件 require 'common.inc.php'; header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:filename=瀏覽記錄.xls"); //bmkj_hits為預(yù)先設(shè)計(jì)好的數(shù)據(jù)庫(kù)表 $sql="desc bmkj_hits"; $result=mysql_query($sql); $rs=mysql_fetch_array($result); echo "< table >< tr >"; //導(dǎo)出表頭(也就是表中擁有的字段) while($rs=mysql_fetch_array($result)){ $t_field[] = $rs['Field']; //Field中的F要大寫,否則沒有結(jié)果 echo "< th >"; ".$rs['Field']." echo "< / th >"; } echo " < / tr >"; //導(dǎo)出100條數(shù)據(jù) $sql = "select * from bmkj_hits order by id desc limit 100"; $result = mysql_query($sql); $rs=mysql_fetch_array($result); while($rs=mysql_fetch_array($result)){ echo "< tr >"; foreach($t_field as $f_key){ echo "< td >"; ".$rs[$f_key]." echo "< / td >"; } echo " < / tr >"; } echo "< / table >"; ?>