如何把把table到入到excel

来源:互联网 发布:运动护具 知乎 编辑:程序博客网 时间:2024/04/25 19:31

原理:
直接输出xls 文件就可以了
Example;

id name date
1 rongge 2004-09-20
2 admin 2004-09-21

保存成a.xls 打开的时候就是excel文件了
注意:
保存的时候 每一个字段中间要一个空格分割,每一行数据之间要换行


代码:
<?php 
//  转载请注明phpteam 
$title  =  "数据库名:test,  数据表:test,  备份日期:"  .  date("Y-m-d  H:i:s"); 
$sep  =  "/t"; 
$crlf  =  "/n"; 
 
$conn  =  @mysql_connect("localhost",  "root",  "")  or  die("不能连接数据库"); 
@mysql_select_db("test",  $conn); 
header("Content-Type:  application/vnd.ms-excel"); 
header("Content-Disposition:  attachment;  filename=test.xls"); 
header("Pragma:  no-cache"); 
header("Expires:  0"); 
 
echo  $title  .  $crlf  .  $crlf; 
 
$query  =  "select  *  from  test"; 
$result  =  mysql_query($query)  or  die(mysql_error()); 
$fields  =  mysql_num_fields($result); 
for($i  =  0;  $i  <  $fields;  $i++)  { 
       echo  mysql_field_name($result,  $i)  .  $sep; 

echo  $crlf; 
while($row  =  mysql_fetch_row($result))  { 
       $line  =  ""; 
       for($i  =  0;  $i<$fields;  $i++)  { 
               $line  .=  $row[$i]  .  $sep; 
       } 
       echo  $line  .  $crlf; 

?>

原创粉丝点击