通過table標(biāo)簽,PHP輸出EXCEL的實現(xiàn)方法
更新時間:2013年07月24日 09:33:11 作者:
以下是利用table標(biāo)簽,對PHP輸出EXCEL的實現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過來參考下
關(guān)鍵代碼:
<?php
header("Content-type:application/vnd.ms-excel");
header("Conten-Disposition:filename=hp.xlsx");
?>
第一句是用來聲明文件內(nèi)容的格式;第二局是用來修改文件名的。如果沒有第二個語句的話,生成的文件將是沒有后綴名的。
實現(xiàn)代碼:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
header("Content-type:application/vnd.ms-excel");
header("Conten-Disposition:filename=hp.xlsx");
?>
<table width="200" border="1">
<tr>
<td colspan="3" align="center">i love you</td>
</tr>
<tr>
<td>編號</td>
<td>姓名</td>
<td>年齡</td>
</tr>
<tr>
<td>1</td>
<td>test</td>
<td>20</td>
</tr>
<tr>
<td>2</td>
<td>test2</td>
<td>22</td>
</tr>
</table>

當(dāng)然,我們很自然的想到了,是否可以把數(shù)據(jù)庫的內(nèi)容也通過這種方式輸出到表格呢?
答案是可以的。
實現(xiàn)代碼:
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=qianshou.xls");
mysql_connect("localhost","root","");
mysql_select_db("test");
mysql_query("SET NAMES GBK");
$query="select * from city ";
$r=mysql_query($query);
?>
<table width="200" border="1">
<tr>
<td colspan="3" align="center">城市列表</td>
</tr>
<tr>
<td align="center">id</td>
<td align="center">p_id</td>
<td align="center">name</td>
</tr>
<?php
while($row=mysql_fetch_assoc($r)){
?>
<tr>
<td><?php echo $row[id] ?></td>
<td><?php echo $row[p_id] ?></td>
<td><?php echo $row[c_name]?></td>
</tr>
<?php
}
?>
</table>
復(fù)制代碼 代碼如下:
<?php
header("Content-type:application/vnd.ms-excel");
header("Conten-Disposition:filename=hp.xlsx");
?>
第一句是用來聲明文件內(nèi)容的格式;第二局是用來修改文件名的。如果沒有第二個語句的話,生成的文件將是沒有后綴名的。
實現(xiàn)代碼:
復(fù)制代碼 代碼如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
header("Content-type:application/vnd.ms-excel");
header("Conten-Disposition:filename=hp.xlsx");
?>
<table width="200" border="1">
<tr>
<td colspan="3" align="center">i love you</td>
</tr>
<tr>
<td>編號</td>
<td>姓名</td>
<td>年齡</td>
</tr>
<tr>
<td>1</td>
<td>test</td>
<td>20</td>
</tr>
<tr>
<td>2</td>
<td>test2</td>
<td>22</td>
</tr>
</table>

當(dāng)然,我們很自然的想到了,是否可以把數(shù)據(jù)庫的內(nèi)容也通過這種方式輸出到表格呢?
答案是可以的。
實現(xiàn)代碼:
復(fù)制代碼 代碼如下:
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=qianshou.xls");
mysql_connect("localhost","root","");
mysql_select_db("test");
mysql_query("SET NAMES GBK");
$query="select * from city ";
$r=mysql_query($query);
?>
<table width="200" border="1">
<tr>
<td colspan="3" align="center">城市列表</td>
</tr>
<tr>
<td align="center">id</td>
<td align="center">p_id</td>
<td align="center">name</td>
</tr>
<?php
while($row=mysql_fetch_assoc($r)){
?>
<tr>
<td><?php echo $row[id] ?></td>
<td><?php echo $row[p_id] ?></td>
<td><?php echo $row[c_name]?></td>
</tr>
<?php
}
?>
</table>
您可能感興趣的文章:
- jQuery通過ajax請求php遍歷json數(shù)組到table中的代碼(推薦)
- jquery+php實現(xiàn)導(dǎo)出datatables插件數(shù)據(jù)到excel的方法
- php循環(huán)table實現(xiàn)一行兩列顯示的方法
- php通過ajax實現(xiàn)雙擊table修改內(nèi)容
- php中hashtable實現(xiàn)示例分享
- PHP+jQuery實現(xiàn)即點即改功能示例
- php輸出表格的實現(xiàn)代碼(修正版)
- php動態(tài)實現(xiàn)表格跨行跨列實現(xiàn)代碼
- PHP+jQuery實現(xiàn)雙擊修改table表格功能示例
相關(guān)文章
PHP CodeBase:將時間顯示為"剛剛""n分鐘/小時前"的方法詳解
本篇文章是對PHP CodeBase:將時間顯示為"剛剛""n分鐘/小時前"的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP導(dǎo)出MySQL數(shù)據(jù)到Excel文件(fputcsv)
經(jīng)常會碰到需要從數(shù)據(jù)庫中導(dǎo)出數(shù)據(jù)到Excel文件,用一些開源的類庫,比如PHPExcel,確實比較容易實現(xiàn),但對大量數(shù)據(jù)的支持很不好,很容易到達(dá)PHP內(nèi)存使用上限。2011-07-07
Apache+PHP+MySQL搭建PHP開發(fā)環(huán)境圖文教程
這篇文章主要介紹了Apache+PHP+MySQL搭建PHP開發(fā)環(huán)境圖文教程,文中通過圖文流程介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
2020-08-08
php使用Jpgraph創(chuàng)建柱狀圖展示年度收支表效果示例
這篇文章主要介紹了php使用Jpgraph創(chuàng)建柱狀圖展示年度收支表效果,結(jié)合完整實例形式分析了Jpgraph創(chuàng)建柱狀圖的具體步驟與操作技巧,需要的朋友可以參考下
2017-02-02 
