php教程 mysql教程如何將資料表轉換為Excel輸出
$xlsname="倒出資訊";
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=$xlsname.xls");
?>
<table>
<tr>
<td ><h3>序號</h3></td>
<td ><h3>內容</h3></td>
<td ><h3>內容</h3></td> </tr></table>
<?
$exapp = new COM("Excel.application") or Die ("Did not connectwww.jzread.com提示你");
$wkb=$exapp->Workbooks->add();
$ex->Application->Visible = 1;
$sheets = $wkb->Worksheets(1); //選擇第一個sheet
$sheets->activate; //啟動
$sheets-> name="Report First page";//更名
//填充儲存格
$maxi=20;
$m=1;
for ($i=1;$i<$maxi;$i++) {
for($n=1; $n<5;$n++){
$cell = $sheets->Cells($i,$n) ; #Select the cell (Row Column number)
$cell->activate;
$cell->value = $m;
$m++;
}
}
$ch = $sheets->chartobjects->add(50, 40, 400, 100); #作圖
$chartje = $ch->chart;
$chartje->activate;
$chartje->ChartType=63;
$selected = $sheets->range("E1:E$maxi");
$chartje->setsourcedata($selected);
存檔
$file_name="D:/test.xls"; 路徑
if (file_exists($file_name)) {@unlink($file_name) or die("www.jzread.com提示你:檔已經存在,並無法刪除!");}
$wkb->SaveAs($file_name); save
$exapp->Quit();
unset($exapp);
echo "生成成功!";
方法三
$a = "帳號,郵箱,性別,寶寶出生日期,省份,真實姓名,郵編,手機,位址 rn";
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=".$Filename);
$Sql = "Select * from oy_use";
$Result = mysql_query($Sql) or die(mysql_error());
while($rs = mysql_fetch_array($Result) ){
// if(get_days($rs['m_time'])<= $Table ){
& nbsp; $a.=$rs['User_Name'].','. $rs['User_Mail'].",". ($rs['User_Baby_Sex']?' 男':'女').",". $rs['User_Baby_Birth'].",";
$a.="".$rs['sheng'].",". $rs['User_rname'].",".$rs['User_Zip'].",".$rs['User_Hand'].",".$rs[' User_Address']." rn ";
// }
}
echo $a;/*
第一種方法是最簡單的要header("Content-Disposition:filename=$xlsname.xls"); 輸出就行了。
第二種複雜但是比較高度,功能就更多了。
第三種就是把資料以cvs格式輸出。