分享

php把数据表导出为Excel表的最简单、最快的方法(不用插件)

 井梅 2017-07-14

<?php

 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);
 //选择编码
 mysql_query("set names ".$cfg_db_language);

 //users表
 $sql = "desc users";

 $res = mysql_query($sql);
 echo "<table><tr>";
 //导出表头(也就是表中拥有的字段)
 while($row = mysql_fetch_array($res)){
  $t_field[] = $row['Field']; //Field中的F要大写,否则没有结果
  echo "<th>".$row['Field']."</th>";
 }
 echo "</tr>";
 //导出100条数据
 $sql = "select * from users limit 100";
 $res = mysql_query($sql);
 while($row = mysql_fetch_array($res)){
  echo "<tr>";
  foreach($t_field as $f_key){
   echo "<td>".$row[$f_key]."</td>";
  }
  echo "</tr>";
 }
 echo "</table>";

?>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多