php 操作excel文件的方法小結(jié)
更新時間:2009年12月31日 14:04:05 作者:
用php生成excel文件的方法,單獨就是用的php,注意一下數(shù)據(jù)的輸入方法即可。
一、php,不用COM,生成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環(huán)境運行上面的代碼,大家就可以看到瀏覽器詢問用戶是否下載excel文檔,點擊保存,硬盤上就多了一個excel的文件,使用excel打開就會看到最終的結(jié)果,怎么樣不錯吧。
其實在做真正的應(yīng)用的時候,大家可以將數(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在處理各種類型文件方面就更加方便了.
二、用PHP將mysql數(shù)據(jù)表轉(zhuǎn)換為excel文件格式
<?php
$DB_Server = "localhost";
$DB_Username = "mydowns";
$DB_Password = "";
$DB_DBName = "mydowns";
$DB_TBLName = "user";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn@#t connect.");
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn@#t select database.");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=mydowns.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");
$now_date = date(@#Y-m-d H:i@#);
$title = "數(shù)據(jù)庫名:$DB_DBName,數(shù)據(jù)表:$DB_TBLName,備份日期:$now_date";
$sql = "Select * from $DB_TBLName";
$ALT_Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn@#t select database");
$result = @mysql_query($sql,$Connect)
or die(mysql_error());
echo("$title\n");
$sep = "\t";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
$i = 0;
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
$i++;
}
return (true);
?>
三、PHP操作excel的一個例子(用COM對象生成excel)
這是對于那些只喜歡簡單處理一下excel朋友來說的
<?php
//定義一個excel文件
$workbook = "C:/My Documents/test.xls";
$sheet = "Sheet1";
//生成一個com對象$ex
$ex = new COM("Excel.sheet") or Die ("連不上?。。?);
//打開一個excel文件
$book = $ex->application->Workbooks->Open($workbook) or Die ("打不開?。?!");
$sheets = $book->Worksheets($sheet);
$sheets->activate;
//獲取一個單元格
$cell = $sheets->Cells(5,5);
$cell->activate;
//給該單元格賦值
$cell->value = 999;
//保存為另一文件newtest.xls
$ex->Application->ActiveWorkbook->SaveAs("newtest.xls");
//關(guān)掉excel,如果想看效果,則注釋掉下面兩行,由用戶手動關(guān)掉excel
$ex->Application->ActiveWorkbook->Close("False");
unset ($ex);
?>
四、php生成EXCEL的東東
可以通過PHP來產(chǎn)生EXCEL檔。
----------------------------
Excel Functions
----------------------------
將下面的代碼存為excel.php ,然后在頁面中包括進來
然后調(diào)用
1. Call xlsBOF()
2. 將一些內(nèi)容寫入到xlswritenunber() 或者 xlswritelabel()中.
3.然后調(diào)用 Call xlsEOF()
也可以用 fwrite 函數(shù)直接寫到服務(wù)器上,而不是用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
?>
復(fù)制代碼 代碼如下:
<?
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環(huán)境運行上面的代碼,大家就可以看到瀏覽器詢問用戶是否下載excel文檔,點擊保存,硬盤上就多了一個excel的文件,使用excel打開就會看到最終的結(jié)果,怎么樣不錯吧。
其實在做真正的應(yīng)用的時候,大家可以將數(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在處理各種類型文件方面就更加方便了.
二、用PHP將mysql數(shù)據(jù)表轉(zhuǎn)換為excel文件格式
復(fù)制代碼 代碼如下:
<?php
$DB_Server = "localhost";
$DB_Username = "mydowns";
$DB_Password = "";
$DB_DBName = "mydowns";
$DB_TBLName = "user";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn@#t connect.");
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn@#t select database.");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=mydowns.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");
$now_date = date(@#Y-m-d H:i@#);
$title = "數(shù)據(jù)庫名:$DB_DBName,數(shù)據(jù)表:$DB_TBLName,備份日期:$now_date";
$sql = "Select * from $DB_TBLName";
$ALT_Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn@#t select database");
$result = @mysql_query($sql,$Connect)
or die(mysql_error());
echo("$title\n");
$sep = "\t";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
$i = 0;
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
$i++;
}
return (true);
?>
三、PHP操作excel的一個例子(用COM對象生成excel)
這是對于那些只喜歡簡單處理一下excel朋友來說的
復(fù)制代碼 代碼如下:
<?php
//定義一個excel文件
$workbook = "C:/My Documents/test.xls";
$sheet = "Sheet1";
//生成一個com對象$ex
$ex = new COM("Excel.sheet") or Die ("連不上?。。?);
//打開一個excel文件
$book = $ex->application->Workbooks->Open($workbook) or Die ("打不開?。?!");
$sheets = $book->Worksheets($sheet);
$sheets->activate;
//獲取一個單元格
$cell = $sheets->Cells(5,5);
$cell->activate;
//給該單元格賦值
$cell->value = 999;
//保存為另一文件newtest.xls
$ex->Application->ActiveWorkbook->SaveAs("newtest.xls");
//關(guān)掉excel,如果想看效果,則注釋掉下面兩行,由用戶手動關(guān)掉excel
$ex->Application->ActiveWorkbook->Close("False");
unset ($ex);
?>
四、php生成EXCEL的東東
可以通過PHP來產(chǎn)生EXCEL檔。
----------------------------
Excel Functions
----------------------------
將下面的代碼存為excel.php ,然后在頁面中包括進來
然后調(diào)用
1. Call xlsBOF()
2. 將一些內(nèi)容寫入到xlswritenunber() 或者 xlswritelabel()中.
3.然后調(diào)用 Call xlsEOF()
也可以用 fwrite 函數(shù)直接寫到服務(wù)器上,而不是用echo 僅僅在瀏覽器上顯示。
復(fù)制代碼 代碼如下:
<?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
?>
相關(guān)文章
php關(guān)聯(lián)數(shù)組快速排序的方法
這篇文章主要介紹了php關(guān)聯(lián)數(shù)組快速排序的方法,涉及php數(shù)組排序的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04PHP頁面跳轉(zhuǎn)操作實例分析(header方法)
這篇文章主要介紹了PHP頁面跳轉(zhuǎn)操作,結(jié)合實例形式對比分析了HTML跳轉(zhuǎn)與php使用header方法跳轉(zhuǎn)的相關(guān)操作技巧與注意事項,并給出了一個跳轉(zhuǎn)的封裝函數(shù)供大家參考,需要的朋友可以參考下2016-09-09php常用數(shù)組array函數(shù)實例總結(jié)【賦值,拆分,合并,計算,添加,刪除,查詢,判斷,排序】
這篇文章主要介紹了php常用數(shù)組array函數(shù),結(jié)合實例形式總結(jié)分析了php常用的數(shù)組操作函數(shù),包括數(shù)組的賦值、拆分、合并、計算、添加、刪除、查詢、判斷、排序等,需要的朋友可以參考下2016-12-12

兩級聯(lián)動select刷新后其值保持不變的實現(xiàn)方法
兩級聯(lián)動select刷新后,select值保持不變即點擊提交按鈕后,頁面select中繼續(xù)維持提交前的值,下面有個不錯的示例,大家可以參考下
2014-01-01 
使用Limit參數(shù)優(yōu)化MySQL查詢的方法
我們在做一些查詢的時候總希望能避免數(shù)據(jù)庫引擎做全表掃描,因為全表掃描時間長,而且其中大部分掃描對客戶端而言是沒有意義的。那么在 MySQL 中有那些方式是可以避免全表掃面的呢?除了我們大家很熟悉的通過使用索引列或分區(qū)等方式來進行查詢的優(yōu)化之外還有那些呢?
2008-11-11