php数据表导出为Excel表的最简单、最快的实现方法
内容摘要
这篇文章主要为大家详细介绍了php数据表导出为Excel表的最简单、最快的实现方法,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!
文章正文
这篇文章主要为大家详细介绍了php数据表导出为Excel表的最简单、最快的实现方法,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!
先定义头部信息,表示输出一个excel。然后再以table的形式把数据库的信息循环的echo出来,就好了。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <code class = "language-php" ><?php /* php教程 www.512Pic.com */ 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>" ; ?></code> |
注:关于php数据表导出为Excel表的最简单、最快的实现方法的内容就先介绍到这里,更多相关文章的可以留意
代码注释