一、不用COM 產生excel
<?<br />header("Content-type:application/vnd.ms-excel");<br />header("Content-Disposition:filename=test.xls");<br />echo "test1/t";<br />echo "test2/t/n";<br />echo "test1/t";<br />echo "test2/t/n";<br />echo "test1/t";<br />echo "test2/t/n";<br />echo "test1/t";<br />echo "test2/t/n";<br />echo "test1/t";<br />echo "test2/t/n";<br />echo "test1/t";<br />echo "test2/t/n";<br />?>
在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在處理各種類型檔案方面就更加方便了.
二、用PHP將mysql資料錶轉換為excel檔案格式
<?php<br />$DB_Server = "localhost";<br />$DB_Username = "mydowns";<br />$DB_Password = "";<br />$DB_DBName = "mydowns";<br />$DB_TBLName = "user";<br />$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)<br />or die("Couldn@#t connect.");<br />$Db = @mysql_select_db($DB_DBName, $Connect)<br />or die("Couldn@#t select database.");<br />$file_type = "vnd.ms-excel";<br />$file_ending = "xls";<br />header("Content-Type: application/$file_type");<br />header("Content-Disposition: attachment; filename=mydowns.$file_ending");<br />header("Pragma: no-cache");<br />header("Expires: 0");<br />$now_date = date(@#Y-m-d H:i@#);<br />$title = "資料庫名:$DB_DBName,資料表:$DB_TBLName,備份日期:$now_date";<br />$sql = "Select * from $DB_TBLName";<br />$ALT_Db = @mysql_select_db($DB_DBName, $Connect)<br />or die("Couldn@#t select database");<br />$result = @mysql_query($sql,$Connect)<br />or die(mysql_error());<br />echo("$title/n");<br />$sep = "/t";<br />for ($i = 0; $i < mysql_num_fields($result); $i++) {<br />echo mysql_field_name($result,$i) . "/t";<br />}<br />print("/n");<br />$i = 0;<br />while($row = mysql_fetch_row($result))<br />{<br />$schema_insert = "";<br />for($j=0; $j<mysql_num_fields($result);$j++)<br />{<br />if(!isset($row[$j]))<br />$schema_insert .= "NULL".$sep;<br />elseif ($row[$j] != "")<br />$schema_insert .= "$row[$j]".$sep;<br />else<br />$schema_insert .= "".$sep;<br />}<br />$schema_insert = str_replace($sep."$", "", $schema_insert);<br />$schema_insert .= "/t";<br />print(trim($schema_insert));<br />print "/n";<br />$i++;<br />}<br />return (true);<br />?>
三、PHP操作excel的一個例子(用COM對象產生excel)
這是對於那些只喜歡簡單處理一下excel朋友來說的
<?php</p><p>//定義一個excel檔案<br />$workbook = "C:/My Documents/test.xls";<br />$sheet = "Sheet1";</p><p>//產生一個com對象$ex<br />$ex = new COM("Excel.sheet") or Die ("連不上!!!");</p><p>//開啟一個excel檔案<br />$book = $ex->application->Workbooks->Open($workbook) or Die ("打不開!!!");</p><p>$sheets = $book->Worksheets($sheet);<br />$sheets->activate;</p><p>//擷取一個儲存格<br />$cell = $sheets->Cells(5,5);<br />$cell->activate;<br />//給該儲存格賦值<br />$cell->value = 999;</p><p>//儲存為另一檔案newtest.xls<br />$ex->Application->ActiveWorkbook->SaveAs("newtest.xls");</p><p>//關掉excel,如果想看效果,則注釋掉下面兩行,由使用者手動關掉excel<br />$ex->Application->ActiveWorkbook->Close("False");<br />unset ($ex);</p><p>?>
四、php產生EXCEL的東東
可以通過PHP來產生EXCEL檔。
----------------------------
Excel Functions
----------------------------
將下面的代碼存為excel.php ,然後在頁面中包括進來
然後調用
1. Call xlsBOF()
2. 將一些內容寫入到xlswritenunber() 或者 xlswritelabel()中.
3.然後調用 Call xlsEOF()
也可以用 fwrite 函數直接寫到伺服器上,而不是用echo 僅僅在瀏覽器上顯示。
<?php
// ----- begin of function library -----
// Excel begin of file header
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
// Excel end of file footer
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
// Function to write a Number (double) into Row, Col
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
// Function to write a label (text) into Row, Col
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
// ----- end of function library -----
?>
//
// To display the contents directly in a MIME compatible browser
// add the following lines on TOP of your PHP file:
<?php
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header (@#Content-type: application/x-msexcel@#);
header ("Content-Disposition: attachment; filename=EmplList.xls" );
header ("Content-Description: PHP/INTERBASE Generated Data" );
//
// the next lines demonstrate the generation of the Excel stream
//
xlsBOF(); // begin Excel stream
xlsWriteLabel(0,0,"This is a label"); // write a label in A1, use for dates too
xlsWriteNumber(0,1,9999); // write a number B1
xlsEOF(); // close the stream
?>