欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php把數(shù)據(jù)表導(dǎo)出為Excel表的最簡單、最快的方法(不用插件)

 更新時間:2014年05月10日 10:31:10   作者:  
很多時候,數(shù)據(jù)庫中的數(shù)據(jù)需要導(dǎo)出成excel,以下是最簡便的方法,不用導(dǎo)出excel的類,即使功能簡單,但是對于沒有復(fù)雜需求的項目“見效快”

先定義頭部信息,表示輸出一個excel。然后再以table的形式把數(shù)據(jù)庫的信息循環(huán)的echo出來,就好了。

復(fù)制代碼 代碼如下:

<?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 配置

 //鏈接數(shù)據(jù)庫
 $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>";
 //導(dǎo)出表頭(也就是表中擁有的字段)
 while($row = mysql_fetch_array($res)){
  $t_field[] = $row['Field']; //Field中的F要大寫,否則沒有結(jié)果
  echo "<th>".$row['Field']."</th>";
 }
 echo "</tr>";
 //導(dǎo)出100條數(shù)據(jù)
 $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>";

?>

相關(guān)文章

最新評論