当前位置:首页 > PHP教程 > php高级应用 > 列表

php导出mysql数据库中为excel代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-11 10:42:02 浏览: 评论:0 

SQL数据库代码如下:

  1. SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
  2. -- 
  3. -- 数据库: `mysqlnew` 
  4. -- 
  5. -- --------------------------------------------------------
  6. -- 
  7. -- 表的结构 `test` 
  8. -- 
  9. CREATE TABLE IF NOT EXISTS `test` ( 
  10.   `id` int(10) NOT NULL auto_increment, 
  11.   `websitename` varchar(200) character set utf8 NOT NULL, 
  12.   `websiteurl` varchar(200) character set utf8 NOT NULL, 
  13.   PRIMARY KEY  (`id`) 
  14. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ; 
  15.  
  16. -- 
  17. -- 导出表中的数据 `test` 
  18. -- 
  19.  
  20. INSERT INTO `test` (`id`, `websitename`, `websiteurl`) VALUES 
  21. (1, '百度', 'http://www.baidu.com'), 
  22. (5, 'google', 'http://www.google.com'), 
  23. (4, '400电话', 'http://www.phpfensi.com'), 
  24. (6, '搜狗', 'www.sogou.com'), 
  25. (7, '必应', 'http://www.phpfensi.com'); 

php实例文件代码如下:

  1. <?php
  2. function xlsBOF() { 
  3.     echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); 
  4.     return; 
  5. } 
  6.  
  7. function xlsEOF() { 
  8.     echo pack("ss", 0x0A, 0x00); 
  9.     return; 
  10. } 
  11.  
  12. function xlsWriteNumber($Row, $Col, $Value) { 
  13.     echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); 
  14.     echo pack("d", $Value); 
  15.     return; 
  16. } 
  17.  
  18. function xlsWriteLabel($Row, $Col, $Value ) { 
  19.     $L = strlen($Value); 
  20.     echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); 
  21.     echo $Value; 
  22.     return; 
  23. } 
  24.  
  25. /** 
  26.  *添加头部信息 
  27.  * @param <type> $rsRec 
  28.  */ 
  29. function addheader($rsRec) { 
  30.     $fieldLen = mysql_num_fields($rsRec); 
  31.     xlsBOF(); 
  32.     $iCell = 0; 
  33.     for($i=0;$i<$fieldLen;$i++) { 
  34.         $fieldname = mysql_field_name($rsRec, $i); 
  35.         xlsWriteLabel(0,$iCell++, iconv("utf-8", "gb2312", $fieldname)); 
  36.     } 
  37.  
  38.  
  39. } 
  40.  
  41. /** 
  42.  *添加记录信息 
  43.  * @param <type> $rsRec 
  44.  */ 
  45. function addData($rsRec) { 
  46.     $xlsRow=1; 
  47.     $fieldLen = mysql_num_fields($rsRec); 
  48.     while($rsone=mysql_fetch_object($rsRec)) { 
  49.         $iCell = 0; 
  50.         for($i=0;$i<$fieldLen;$i++) { 
  51.             $fieldname = mysql_field_name($rsRec, $i); 
  52.             $fieldnameValue = $rsone->$fieldname; 
  53.             xlsWriteLabel($xlsRow,$iCell++,iconv("utf-8", "gb2312", $fieldnameValue)); 
  54.         } 
  55.  
  56.         $xlsRow++; 
  57.     } 
  58.     xlsEOF(); 
  59.     exit; 
  60. } 
  61.  
  62. if(isset($_POST["tableName"])) { 
  63.  
  64.     $con = mysql_connect("localhost", "root", "vertrigo"); 
  65.  
  66.     $result=mysql_db_query("mysqlnew","select * from ".$_POST["tableName"]); 
  67.  
  68. // 文件头 
  69.     header("Pragma: public"); 
  70.     header("Expires: 0"); 
  71.     header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
  72.     header("Content-Type: application/force-download"); 
  73.     header("Content-Type: application/octet-stream"); 
  74.     header("Content-Type: application/download"); 
  75.     header("Content-Disposition: attachment;filename=test.xls "); 
  76.     header("Content-Transfer-Encoding: binary "); 
  77.  
  78. // 向表中添加数据 
  79.     addheader($result) ; 
  80.     addData($result) ; 
  81.  
  82.     mysql_close($con); 
  83.  
  84. } 
  85.  
  86. ?> 
  87. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  88. <html> 
  89.     <head> 
  90.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  91.         <title>excel信息导出</title> 
  92.     </head> 
  93.     <body> 
  94.         <div> 
  95.             <form name="form1" method="post" action="" onSubmit=""> 
  96.                 <input type="text" name="tableName" value="" /> 
  97.                 <br> 
  98.                  
  99.                 <input class="ccc" name="" type="submit" value="提交" /> 
  100.             </form> 
  101.         </div> 
  102.     </body> 
  103. </html> 

生成excel文件内容:

id websitename websiteurl     

1 百度 http://www.baidu.com

5 google http://www.google.com

4 400电话 http://www.phpfensi.com

6 搜狗 www.sogou.com

7 必应 http://www.phpfensi.com

Tags: PHP导出excel mysql数据库导出excel

分享到: