0712-2888027 189-8648-0214
微信公眾號(hào)

孝感風(fēng)信網(wǎng)絡(luò)科技有限公司微信公眾號(hào)

當(dāng)前位置:主頁(yè) > 技術(shù)支持 > PHP > php導(dǎo)出excel文件

php導(dǎo)出excel文件

時(shí)間:2024-09-19來(lái)源:風(fēng)信官網(wǎng) 點(diǎn)擊: 1084次

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

<?header("Content-type:application/vnd.ms-excel");

header("Content-Disposition:filename=test.xls");

echo "test1 ";

echo "test2
";

echo "test1 ";

echo "test2
";

echo "test1 ";

echo "test2
";

echo "test1 ";

echo "test2
";

echo "test1 ";

echo "test2
";

echo "test1 ";

echo "test2
";

?>

在 php環(huán)境運(yùn)行上面的代碼,大家就可以看到瀏覽器詢問(wèn)用戶是否下載excel文檔,點(diǎn)擊保存,硬盤(pán)上就多了一個(gè)excel的文件,使用excel打開(kāi) 就會(huì)看到最終的結(jié)果,怎么樣不錯(cuò)吧?! ∑鋵?shí)在做真正的應(yīng)用的時(shí)候,大家可以將數(shù)據(jù)從數(shù)據(jù)庫(kù)中取出,然后按照每一列數(shù)據(jù)結(jié)束后加 ,每一行數(shù)據(jù)結(jié)束后加
的 方法echo出來(lái),在php的開(kāi)頭用header("Content-type:application/vnd.ms-excel");表示輸出的是 excel文件,用header("Content-Disposition:filename=test.xls");表示輸出的文件名為 text.xls。這樣就ok了?! ∥覀兏梢孕薷膆eader讓他輸出更多格式的文件,這樣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ù)庫(kù)名:$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
");

$sep = " ";

for ($i = 0; $i < mysql_num_fields($result); $i++) {

echo mysql_field_name($result,$i) . " ";

}

print("
");

$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 .= " ";

print(trim($schema_insert));

print "
";

$i++;

}

return (true);

?>

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

這是對(duì)于那些只喜歡簡(jiǎn)單處理一下excel朋友來(lái)說(shuō)的(此方法只能在windows平臺(tái)上運(yùn)行)<?php//定義一個(gè)excel文件

$workbook = "C:/My Documents/test.xls";

$sheet = "Sheet1";//生成一個(gè)com對(duì)象$ex

$ex = new COM("Excel.sheet") or Die ("連不上!??!");//打開(kāi)一個(gè)excel文件

$book = $ex->application->Workbooks->Open($workbook) or Die ("打不開(kāi)!??!");$sheets = $book->Worksheets($sheet);

$sheets->activate;//獲取一個(gè)單元格

$cell = $sheets->Cells(5,5);

$cell->activate;

//給該單元格賦值

$cell->value = 999;//保存為另一文件newtest.xls

$ex->Application->ActiveWorkbook->SaveAs("newtest.xls");//關(guān)掉excel,如果想看效果,則注釋掉下面兩行,由用戶手動(dòng)關(guān)掉excel

$ex->Application->ActiveWorkbook->Close("False");

unset ($ex);?>

四、php生成EXCEL的東東

可以通過(guò)PHP來(lái)產(chǎn)生EXCEL檔。

----------------------------Excel Functions----------------------------

將下面的代碼存為excel.php ,然后在頁(yè)面中包括進(jìn)來(lái)然后調(diào)用1. Call xlsBOF()

2. 將一些內(nèi)容寫(xiě)入到xlswritenunber() 或者 xlswritelabel()中.

3.然后調(diào)用 Call xlsEOF()也可以用 fwrite 函數(shù)直接寫(xiě)到服務(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 footerfunction

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 -----

?>

<?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" );

xlsBOF();

streamxlsWriteLabel(0,0,"This is a label");

tooxlsWriteNumber(0,1,9999);

B1xlsEOF();

?>

熱門(mén)關(guān)鍵詞: php excel文件

您可能感興趣的相關(guān)文章:

欄目列表
推薦內(nèi)容
熱點(diǎn)內(nèi)容
展開(kāi)