PHP导出Excel简单实例

<?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=table.xls");
?>
  <table border="1">
    <tr>
      <td>t00</td><td>t01</td><td>t02</td>
    </tr>
    <tr>
      <td>t10</td><td>t11</td><td>t12</td>
    </tr>
    <tr>
      <td>t20</td><td>t21</td><td>t22</td>
    </tr>
  </table>

只要有则文档相当于xls文件:

header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=table.xls");

需要写入了再导出,可参考以下例子:

	//文件写入
 $files="mei_excel_write.php";

 if(!is_writable($files)){    		//判断文件是否可写
  echo"<font color=red>不可写</font>";
 }else{
  echo"<font color=green>可写</font>";
 }
$time=date("Y-m-d H:i:s");
 if(isset($_POST['write'])){  		//把信息写入文件
  $str="<?php";
  $str.="\n";
  $str.="header(\"Content-type:application/vnd.ms-excel\");";
  $str.="\n";
  $str.="header(\"Content-Disposition:attachment;filename=".$time.".xls\");";
    $str.="\n";
  $str.="?>";
 $str.="\n";
$str.="<table border=\"1\">";
$str.="<tr>";
$str.="<td>ID</td>";
$str.=" </tr>";
  while ($row = mysql_fetch_array($query))					{ 
	$str.=" <tr>";	
	$str.="<td>".$st."</td>";  
	$str.=" </tr>"; 	                 
   }                   

  

posted @ 2012-11-28 18:42  tinyphp  Views(994)  Comments(0Edit  收藏  举报