当前位置:Gxlcms > PHP教程 > php操作excel文件的方法小结_php技巧

php操作excel文件的方法小结_php技巧

时间:2021-07-01 10:21:17 帮助过:60人阅读

一、php,不用COM,生成excel文件
代码如下:
  1. <br><!--? <BR-->header("Content-type:application/vnd.ms-excel"); <br>header("Content-Disposition:filename=test.xls"); <br>echo "test1\t"; <br>echo "test2\t\n"; <br>echo "test1\t"; <br>echo "test2\t\n"; <br>echo "test1\t"; <br>echo "test2\t\n"; <br>echo "test1\t"; <br>echo "test2\t\n"; <br>echo "test1\t"; <br>echo "test2\t\n"; <br>echo "test1\t"; <br>echo "test2\t\n"; <br>?> <br> <br>  在php环境运行上面的代码,大家就可以看到浏览器询问用户是否下载excel文档,点击保存,硬盘上就多了一个excel的文件,使用excel打开就会看到最终的结果,怎么样不错吧。 <br>  其实在做真正的应用的时候,大家可以将数据从数据库中取出,然后按照每一列数据结束后加
  2. ,每一行数据结束后加
  3. 的方法echo出来,在php的开头用header("Content-type:application/vnd.ms-excel");表示
输出的是excel文件,用header("Content-Disposition:filename=test.xls");表示输出的文件名为text.xls。这样就ok了。
  我们更可以修改header让他输出更多格式的文件,这样php在处理各种类型文件方面就更加方便了.
二、用PHP将mysql数据表转换为excel文件格式
代码如下:
  1. <br><!--?php <BR-->$DB_Server = "localhost"; <br>$DB_Username = "mydowns"; <br>$DB_Password = ""; <br>$DB_DBName = "mydowns"; <br>$DB_TBLName = "user"; <br>$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) <br>or die("Couldn@#t connect."); <br>$Db = @mysql_select_db($DB_DBName, $Connect) <br>or die("Couldn@#t select database."); <br>$file_type = "vnd.ms-excel"; <br>$file_ending = "xls"; <br>header("Content-Type: application/$file_type"); <br>header("Content-Disposition: attachment; filename=mydowns.$file_ending"); <br>header("Pragma: no-cache"); <br>header("Expires: 0"); <br>$now_date = date(@#Y-m-d H:i@#); <br>$title = "数据库名:$DB_DBName,数据表:$DB_TBLName,备份日期:$now_date"; <br>$sql = "Select * from $DB_TBLName"; <br>$ALT_Db = @mysql_select_db($DB_DBName, $Connect) <br>or die("Couldn@#t select database"); <br>$result = @mysql_query($sql,$Connect) <br>or die(mysql_error()); <br>echo("$title\n"); <br>$sep = "\t"; <br>for ($i = 0; $i < mysql_num_fields($result); $i++) { <br>echo mysql_field_name($result,$i) . "\t"; <br>} <br>print("\n"); <br>$i = 0; <br>while($row = mysql_fetch_row($result)) <br>{ <br>$schema_insert = ""; <br>for($j=0; $j<mysql_num_fields($result);$j++) <br="">{ <br>if(!isset($row[$j])) <br>$schema_insert .= "NULL".$sep; <br>elseif ($row[$j] != "") <br>$schema_insert .= "$row[$j]".$sep; <br>else <br>$schema_insert .= "".$sep; <br>} <br>$schema_insert = str_replace($sep."$", "", $schema_insert); <br>$schema_insert .= "\t"; <br>print(trim($schema_insert)); <br>print "\n"; <br>$i++; <br>} <br>return (true); <br>?> <br> <br><strong>三、PHP操作excel的一个例子(用COM对象生成excel)</strong> <br>这是对于那些只喜欢简单处理一下excel朋友来说的 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->//定义一个excel文件 <br>$workbook = "C:/My Documents/test.xls"; <br>$sheet = "Sheet1"; <br>//生成一个com对象$ex <br>$ex = new COM("Excel.sheet") or Die ("连不上!!!"); <br>//打开一个excel文件 <br>$book = $ex->application->Workbooks->Open($workbook) or Die ("打不开!!!"); <br>$sheets = $book->Worksheets($sheet); <br>$sheets->activate; <br>//获取一个单元格 <br>$cell = $sheets->Cells(5,5); <br>$cell->activate; <br>//给该单元格赋值 <br>$cell->value = 999; <br>//保存为另一文件newtest.xls <br>$ex->Application->ActiveWorkbook->SaveAs("newtest.xls"); <br>//关掉excel,如果想看效果,则注释掉下面两行,由用户手动关掉excel <br>$ex->Application->ActiveWorkbook->Close("False"); <br>unset ($ex); <br>?> <br> <br><strong>四、php生成EXCEL的东东 <br></strong>可以通过PHP来产生EXCEL档。 <br>---------------------------- <br>Excel Functions <br>---------------------------- <br>将下面的代码存为excel.php ,然后在页面中包括进来 <br>然后调用 <br>1. Call xlsBOF() <br>2. 将一些内容写入到xlswritenunber() 或者 xlswritelabel()中. <br>3.然后调用 Call xlsEOF() <br>也可以用 fwrite 函数直接写到服务器上,而不是用echo 仅仅在浏览器上显示。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->// ----- begin of function library ----- <br>// Excel begin of file header <br>function xlsBOF() { <br>echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); <br>return; <br>} <br>// Excel end of file footer <br>function xlsEOF() { <br>echo pack("ss", 0x0A, 0x00); <br>return; <br>} <br>// Function to write a Number (double) into Row, Col <br>function xlsWriteNumber($Row, $Col, $Value) { <br>echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); <br>echo pack("d", $Value); <br>return; <br>} <br>// Function to write a label (text) into Row, Col <br>function xlsWriteLabel($Row, $Col, $Value ) { <br>$L = strlen($Value); <br>echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); <br>echo $Value; <br>return; <br>} <br>// ----- end of function library ----- <br>?> <br>// <br>// To display the contents directly in a MIME compatible browser <br>// add the following lines on TOP of your PHP file: <br><!--?php <BR-->header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); <br>header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT"); <br>header ("Cache-Control: no-cache, must-revalidate"); <br>header ("Pragma: no-cache"); <br>header (@#Content-type: application/x-msexcel@#); <br>header ("Content-Disposition: attachment; filename=EmplList.xls" ); <br>header ("Content-Description: PHP/INTERBASE Generated Data" ); <br>// <br>// the next lines demonstrate the generation of the Excel stream <br>// <br>xlsBOF(); // begin Excel stream <br>xlsWriteLabel(0,0,"This is a label"); // write a label in A1, use for dates too <br>xlsWriteNumber(0,1,9999); // write a number B1 <br>xlsEOF(); // close the stream <br>?> <br> </li></ol></pre></li></ol></pre></mysql_num_fields($result);$j++)>

人气教程排行