php操作excel文件的方法小結_第1頁
php操作excel文件的方法小結_第2頁
php操作excel文件的方法小結_第3頁
php操作excel文件的方法小結_第4頁
php操作excel文件的方法小結_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

本文格式為Word版,下載可任意編輯——php操作excel文件的方法小結php操作excel文件的方法小結

php操作excel文件的方法有哪些?就跟隨我一起去了解下吧,想了解更多相關信息請持續(xù)關注我們我!

一、php,不用COM,生成excel文件

復制代碼代碼如下:

?

headerContent-type:application/vnd.ms-excel;

headerContent-Disposition:filename=test.xls;

echotest1t;

echotest2tn;

echotest1t;

echotest2tn;

echotest1t;

echotest2tn;

echotest1t;

echotest2tn;

echotest1t;

echotest2tn;

echotest1t;

echotest2tn;

?

在php環(huán)境運行上面的代碼,大家就可以看到欣賞器詢問用戶是否下載excel文檔,點擊保存,硬盤上就多了一個excel的`文件,使用excel開啟就會看到最終的結果,怎么樣不錯吧。

其實在做真正的應用的時候,大家可以將數(shù)據(jù)從數(shù)據(jù)庫中取出,然后按照每一列數(shù)據(jù)終止后加t,每一行數(shù)據(jù)終止后加n的方法echo出來,在php的開頭用headerContent-type:application/vnd.ms-excel;表示輸出的是excel文件,用headerContent-Disposition:filename=test.xls;表示輸出的文件名為text.xls。這樣就ok了。

我們更可以修改header讓他輸出更多格式的文件,這樣php在處理各種類型文件方面就更加便當了.

二、用PHP將mysql數(shù)據(jù)表轉換為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

ordieCouldn@#tconnect.;

$Db=@mysql_select_db$DB_DBName,$Connect

ordieCouldn@#tselectdatabase.;

$file_type=vnd.ms-excel;

$file_ending=xls;

headerContent-Type:application/$file_type;

headerContent-Disposition:attachment;filename=mydowns.$file_ending;

headerPragma:no-cache;

headerExpires:0;

$now_date=date@#Y-m-dH: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

ordieCouldn@#tselectdatabase;

$result=@mysql_query$sql,$Connect

ordiemysql_error;

echo$titlen;

$sep=t;

for$i=0;$imysql_num_fields$result;$i++

echomysql_field_name$result,$i.t;

printn;

$i=0;

while$row=mysql_fetch_row$result

php操作excel文件的方法小結

$schema_insert=;

for$j=0;$jmysql_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;

printtrim$schema_insert;

printn;

$i++;

returntrue;

?

三、PHP操作excel的一個例子(用COM對象生成excel)

這是對于那些只熱愛簡樸處理一下excel摯友來說的

復制代碼代碼如下:

?php

//定義一個excel文件

$workbook=C:/MyDocuments/test.xls;

$sheet=Sheet1;

//生成一個com對象$ex

$ex=newCOMExcel.sheetorDie連不上?。。?

//開啟一個excel文件

$book=$ex-application-Workbooks-Open$workbookorDie打不開?。。?

$sheets=$book-Worksheets$sheet;

$sheets-activate;

//獲取一個單元格

$cell=$sheets-Cells5,5;

$cell-activate;

//給該單元格賦值

$cell-value=999;

//保存為另一文件newtest.xls

$ex-Application-ActiveWorkbook-SaveAsnewtest.xls;

//關掉excel,假設想看效果,那么解釋掉下面兩行,由用戶手動關掉excel

$ex-Application-ActiveWorkbook-CloseFalse;

unset$ex;

?

四、php生成EXCEL的東東

可以通過PHP來產(chǎn)生EXCEL檔。

ExcelFunctions

將下面的代碼存為excel.php,然后在頁面中包括進來

然后調用

1.CallxlsBOF

2.將一些內容寫入到xlswritenunber或者xlswritelabel中.

3.然后調用CallxlsEOF

也可以用fwrite函數(shù)直接寫到服務器上,而不是用echo僅僅在欣賞器上顯示。

復制代碼代碼如下:

?php

//beginoffunctionlibrary

//Excelbeginoffileheader

functionxlsBOF

echopackssssss,0x809,0x8,0x0,0x10,0x0,0x0;

return;

//Excelendoffilefooter

functionxlsEOF

echopackss,0x0A,0x00;

return;

//FunctiontowriteaNumberdoubleintoRow,Col

functionxlsWriteNumber$Row,$Col,$Value

echopacksssss,0x203,14,$Row,$Col,0x0;

echopackd,$Value;

return;

//FunctiontowritealabeltextintoRow,Col

functionxlsWriteLabel$Row,$Col,$Value

$L=strlen$Value;

echopackssssss,0x204,8+$L,$Row,$Col,0x0,$L;

echo$Value;

return;

//endoffunctionlibrary

?

//

//TodisplaythecontentsdirectlyinaMIMEcompatiblebrowser

//addthefollowinglinesonTOPofyourPHPfile:

?php

headerExpires:Mon,26Jul199705:00:00GMT;

headerLast-Modified:.gmdateD,dMYH:i:s.GMT;

headerCache-Control:no-cache,must-revalidate;

headerPragma:no-cache;

header@#Content-type:application/x-msexcel@#;

headerContent-Disposition:attachment;filename=EmplList.xls;

headerContent-Description:PHP/INTERBASEGeneratedData;

//

//thenextlinesdemonstratethegenera

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論