zl程序教程

您现在的位置是:首页 >  工具

当前栏目

php操作excel文件的方法小结

ExcelPHP文件方法 操作 小结
2023-06-13 09:14:15 时间
一、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环境运行上面的代码,大家就可以看到浏览器询问用户是否下载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
$DB_Server="localhost";
$DB_Username="mydowns";
$DB_Password="";
$DB_DBName="mydowns";
$DB_TBLName="user";
$Connect=@mysql_connect($DB_Server,$DB_Username,$DB_Password)
ordie("Couldn@#tconnect.");
$Db=@mysql_select_db($DB_DBName,$Connect)
ordie("Couldn@#tselectdatabase.");
$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-dH:i@#);
$title="数据库名:$DB_DBName,数据表:$DB_TBLName,备份日期:$now_date";
$sql="Select*from$DB_TBLName";
$ALT_Db=@mysql_select_db($DB_DBName,$Connect)
ordie("Couldn@#tselectdatabase");
$result=@mysql_query($sql,$Connect)
ordie(mysql_error());
echo("$title\n");
$sep="\t";
for($i=0;$i<mysql_num_fields($result);$i++){
echomysql_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:/MyDocuments/test.xls";
$sheet="Sheet1";
//生成一个com对象$ex
$ex=newCOM("Excel.sheet")orDie("连不上!!!");
//打开一个excel文件
$book=$ex->application->Workbooks->Open($workbook)orDie("打不开!!!");
$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档。
----------------------------
ExcelFunctions
----------------------------
将下面的代码存为excel.php,然后在页面中包括进来
然后调用
1.CallxlsBOF()
2.将一些内容写入到xlswritenunber()或者xlswritelabel()中.
3.然后调用CallxlsEOF()
也可以用fwrite函数直接写到服务器上,而不是用echo仅仅在浏览器上显示。
复制代码代码如下:
<?php
//-----beginoffunctionlibrary-----
//Excelbeginoffileheader
functionxlsBOF(){
echopack("ssssss",0x809,0x8,0x0,0x10,0x0,0x0);
return;
}
//Excelendoffilefooter
functionxlsEOF(){
echopack("ss",0x0A,0x00);
return;
}
//FunctiontowriteaNumber(double)intoRow,Col
functionxlsWriteNumber($Row,$Col,$Value){
echopack("sssss",0x203,14,$Row,$Col,0x0);
echopack("d",$Value);
return;
}
//Functiontowritealabel(text)intoRow,Col
functionxlsWriteLabel($Row,$Col,$Value){
$L=strlen($Value);
echopack("ssssss",0x204,8+$L,$Row,$Col,0x0,$L);
echo$Value;
return;
}
//-----endoffunctionlibrary-----
?>
//
//TodisplaythecontentsdirectlyinaMIMEcompatiblebrowser
//addthefollowinglinesonTOPofyourPHPfile:
<?php
header("Expires:Mon,26Jul199705:00:00GMT");
header("Last-Modified:".gmdate("D,dMYH: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/INTERBASEGeneratedData");
//
//thenextlinesdemonstratethegenerationoftheExcelstream
//
xlsBOF();//beginExcelstream
xlsWriteLabel(0,0,"Thisisalabel");//writealabelinA1,usefordatestoo
xlsWriteNumber(0,1,9999);//writeanumberB1
xlsEOF();//closethestream
?>