很多时候,数据库中的数据需要导出成excel,以下是最简便的方法,不用导出excel的类,即使功能简单,但是对于没有复杂需求的项目“见效快”.
先定义头部信息,表示输出一个excel,然后再以table的形式把数据库的信息循环的echo出来,就好了,代码如下:
- <?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';
-
-
-
-
-
- $link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
-
- mysql_select_db($cfg_dbname);
-
-
-
- mysql_query("set names ".$cfg_db_language);
-
-
-
- $sql = "desc users";
-
- $res = mysql_query($sql);
-
- echo "<table><tr>";
-
-
-
- while($row = mysql_fetch_array($res)){
-
- $t_field[] = $row['Field'];
-
- echo "<th>".$row['Field']."</th>";
-
- }
-
- echo "</tr>";
-
-
-
- $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>";
-
- ?>
|