<? //生成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";
?>
//改動相應(yīng)文件頭就可以輸出.doc .xls等文件格式了
在php環(huán)境運(yùn)行上面的代碼,大家就可以看到瀏覽器詢問用戶是否下載excel文檔,點(diǎn)擊保存,硬盤上就多了一個(gè)excel的文件,使用excel打開就會看到最終的結(jié)果,怎么樣不錯(cuò)吧。
其實(shí)在做真正的應(yīng)用的時(shí)候,大家可以將數(shù)據(jù)從數(shù)據(jù)庫中取出,然后按照每一列數(shù)據(jù)結(jié)束后加\t,每一行數(shù)據(jù)結(jié)束后加\n的方法echo出來,在php的開頭用header("Content-type:application/vnd.ms-excel");表示輸出的是excel文件,用header("Content-Disposition:filename=test.xls");表示輸出的文件名為text.xls。這樣就ok了。
我們更可以修改header讓他輸出更多格式的文件,這樣php在處理各種類型文件方面就更加方便了
哈哈,托你的福,我已經(jīng)知道怎么解決了,你把下面著段代碼存為.xls文件看看,知道該怎么做了吧?
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns=" <head>
<meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name></x:Name>
<x:WorksheetOptions>
<x:DisplayGridlines/>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml><![endif]-->
</head>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td align="center" class="big6" >Users (except the ones disabled)</td></tr>
<tr><td class="greyborder">
<table border="1" align="center" width="100%" cellpadding="1" cellspacing="1">
<tr align="center">
<td class="TableTopHeader" nowrap>
Login </td>
<td class="TableTopHeader" nowrap>
Name </td>
<td class="TableTopHeader" width="3%" nowrap>
EMail </td>
<td class="TableTopHeader" nowrap>
Department </td>
<td class="TableTopHeader" nowrap>
Position </td>
<td class="TableTopHeader" nowrap width="1%">
Level </td>
<td class="TableTopHeader">Address</td>
<td class="TableTopHeader">Phone</td>
</tr>
<tr class="even">
<td class="mtlist"> joesen</td>
<td class="mtlist"> hong joesen</td>
<td class="mtlist" >allfu@163.net</td>
<td class="mtlist"> Software developments</td>
<td class="mtlist"> System Administrator</td>
<td class="mtlist"> Admin</td>
<td class="mtlist"> </td>
<td class="mtlist"> </td>
</tr>
</table>
</td></tr>
</table>
posted on 2005-05-31 13:16
楚客 閱讀(2612)
評論(0) 編輯 收藏 所屬分類:
HTML 、
PHP