php 操作excel文件的方法小结

可以经过PHP来发生EXCEL档.teaman翻译 —————————- 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, 0x0State of Qatar; return; } // Excel end of file footer
function xlsEOF(卡塔尔国 { echo pack(“ss”, 0x0A, 0x00State of Qatar; return; } // Function
to write a Number (double卡塔尔 into Row, Col function xlsWriteNumber($Row,
$Col, $ValueState of Qatar { echo pack(“sssss”, 0x203, 14, $Row, $Col, 0x0State of Qatar; echo
pack(“d”, $Value卡塔尔国; return; } // Function to write a label (textState of Qatar into
Row, Col function xlsWriteLabel($Row, $Col, $Value State of Qatar { $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 1998 05:00:00 GMT”卡塔尔(قطر‎; header (“Last-Modified: ” . gmdate(“D,d M
YH:i:s”卡塔尔(قطر‎ . ” 丙胺博莱霉素T”卡塔尔国; header (“Cache-Control: no-cache, must-revalidate”卡塔尔;
header (“Pragma: no-cache”卡塔尔; header (Content-type:
application/x-msexcel卡塔尔国; header (“Content-Disposition: attachment;
filename=EmplList.xls” State of Qatar; 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(State of Qatar; //
close the stream ?

一、php,不用COM,生成excel文件
复制代码 代码如下:

header(“Content-type:application/vnd.ms-excel”);
header(“Content-Disposition:filename=test.xls”);
echo “test1t”;
echo “test2tn”;
echo “test1t”;
echo “test2tn”;
echo “test1t”;
echo “test2tn”;
echo “test1t”;
echo “test2tn”;
echo “test1t”;
echo “test2tn”;
echo “test1t”;
echo “test2tn”;
?>

  在php意况运营方面包车型客车代码,大家就足以看见浏览器询问客户是还是不是下载excel文书档案,点击保存,硬盘上就多了一个excel的文件,使用excel张开就拜谒到最终的结果,怎么着不错啊。
  其实在抓牢在的利用的时候,大家能够将数据从数据库中收取,然后根据每一列数据截至后加t,每一行数据停止后加n的方法echo出来,在php的初始用header(“Content-type:application/vnd.ms-excel”State of Qatar;表示输出的是excel文件,用header(“Content-Disposition:filename=test.xls”State of Qatar;表示输出的公文名叫text.xls。那样就ok了。
  大家更能够订正header让他输出越多格式的文件,那样php在管理各体系型文件上面就一发有利了.
二、用PHP将mysql数据表转变为excel文件格式 复制代码 代码如下:

$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 =
“数据库名:$DB_DBName,数据表:$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(“$titlen”);
$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{
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有相恋的人来讲的
复制代码 代码如下:

//定义贰个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”);
//关掉excel,假设想看效率,则注释掉上边两行,由客商手动关掉excel
$ex->Application->ActiveWorkbook->Close(“False”);
unset ($ex);
?>

**四、php生成EXCEL的东东

**能够透过PHP来发生EXCEL档。

Excel Functions

将下边包车型客车代码存为excel.php ,然后在页面中包含进来
接下来调用

  1. Call xlsBOF()
  2. 将部分内容写入到xlswritenunber(卡塔尔 恐怕 xlswritelabel(卡塔尔(قطر‎中.
    3.然后调用 Call xlsEOF(卡塔尔(قطر‎
    也得以用 fwrite 函数直接写到服务器上,并非用echo
    仅仅在浏览器上展现。
    复制代码 代码如下:

// —– 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:

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

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图