JARON.CN 2003年10月26日<script src="/cms-server/ArticleViewTimes.asp?id=101256" type="text/javascript"> </script> 閱讀:257次
php生成excel文檔太簡單了,估計大家都會用到,所以共享出來。
大家來看代碼:
<?
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=test.xls");
echo "test1/t";
echo "test2/t/n";
echo "test1/t";
echo "test2/t/n";
echo "test1/t";
echo "test2/t/n";
echo "test1/t";
echo "test2/t/n";
echo "test1/t";
echo "test2/t/n";
echo "test1/t";
echo "test2/t/n";
?>
在php環境運行上面的代碼,大家就可以看到瀏覽器詢問用戶是否下載excel文檔,點擊保存,硬盤上就多了一個excel的文件,使用excel打開就會看到最終的結果,怎麼樣不錯吧。
其實在做真正的應用的時候,大家可以將數據從數據庫中取出,然後按照每一列數據結束後加/t,每一行數據結束後加/n的方法echo出來,在php的開頭用header("Content-type:application/vnd.ms-excel");表示輸出的是excel文件,用header("Content-Disposition:filename=test.xls");表示輸出的文件名爲text.xls。這樣就ok了。
我們更可以修改header讓他輸出更多格式的文件,這樣php在處理各種類型文件方面就更加方便了.