【php怎么把数据表中的数据导出到excel】php怎么把数据表中的数据导出到excel表中

更新时间:2019-05-02    来源:excel    手机版     字体:

【www.bbyears.com--excel】

先定义头部信息,表示输出一个excel。然后再以table的形式把数据库的信息循环的echo出来,就好了。

 代码如下

 

 header("Content-type:application/vnd.ms-excel");

 header("Content-Disposition:filename=xls_region.xls");

 $cfg_dbhost = "localhost";

 $cfg_dbname = "testdb";

 $cfg_dbuser = "root";

 $cfg_dbpwd = "root";

 $cfg_db_language = "utf8";

 // END 配置

 


 //链接数据库

 $link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);

 mysql_select_db($cfg_dbname);

 //选择编码 www.111Cn.net

 mysql_query("set names ".$cfg_db_language);

 

 //users表

 $sql = "desc users";

 

 $res = mysql_query($sql);

 echo "";

 //导出表头(也就是表中拥有的字段)

 while($row = mysql_fetch_array($res)){

  $t_field[] = $row["Field"]; //Field中的F要大写,否则没有结果

  echo "".$row["Field"]."";

 }

 echo "";

 //导出100条数据

 $sql = "select * from users limit 100";

 $res = mysql_query($sql);

 while($row = mysql_fetch_array($res)){

  echo "";

  foreach($t_field as $f_key){

   echo "".$row[$f_key]."";

  }

  echo "";

 }

 echo "";

 

?>

 

本文来源:http://www.bbyears.com/bangongshuma/49846.html

热门标签

更多>>

本类排行