php语言 百分网手机站

php操作excel文件的方法小结(2)

时间:2020-08-06 09:24:33 php语言 我要投稿

php操作excel文件的方法小结

  $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:/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 仅仅在浏览器上显示。

  复制代码 代码如下:

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

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

  //

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

  ?> </p

【php操作excel文件的方法小结】相关文章:

PHP文件怎么操作09-07

php文件操作函数解释09-07

PHP常用的文件操作函数09-09

PHP基础学习之文件操作09-04

PHP生成Excel报表的方法09-08

PHP常用的文件操作函数有哪些09-08

php读取文件内容的方法09-08

PHP读取文件的正确方法09-04

恢复损坏的Excel文件的方法09-17

PHP生成Excel报表的方法介绍09-04