国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP將mysql數據庫導出為excel表

2024-05-04 21:49:09
字體:
來源:轉載
供稿:網友

利用php導出mysql數據庫為excel表格的方法很多,最簡單的就直接使用php fputcsv函數了,還有就是直接輸入csv格式也是可以了,要生成excel標準格式我們需使用第三方插件了.

方法一,利用fputcsv,代碼如下:

  1. // 輸出Excel文件頭,可把user.csv換成你要的文件名  
  2. header('Content-Type: application/vnd.ms-excel');  
  3. header('Content-Disposition: attachment;filename="user.csv"');  
  4. header('Cache-Control: max-age=0'); 
  5.  
  6. // 從數據庫中獲取數據,為了節省內存,不要把數據一次性讀到內存,從句柄中一行一行讀即可  
  7. $sql = 'select * from tbl where ……';  
  8. $stmt = $db->query($sql); 
  9.  
  10. // 打開PHP文件句柄,php://output 表示直接輸出到瀏覽器  
  11. $fp = fopen('php://output''a'); 
  12.  
  13. // 輸出Excel列名信息  
  14. $head = array('姓名''性別''年齡''Email''電話''……');  
  15. foreach ($head as $i => $v) {  
  16. // CSV的Excel支持GBK編碼,一定要轉換,否則亂碼  
  17. $head[$i] = iconv('utf-8''gbk'$v);  
  18.  
  19. // 將數據通過fputcsv寫到文件句柄  
  20. fputcsv($fp$head); 
  21.  
  22. // 計數器  
  23. $cnt = 0;  
  24. // 每隔$limit行,刷新一下輸出buffer,不要太大,也不要太小  
  25. $limit = 100000; 
  26.  
  27. // 逐行取出數據,不浪費內存  
  28. while ($row = $stmt->fetch(Zend_Db::FETCH_NUM)) { 
  29. //開源代碼Vevb.com 
  30. $cnt ++;  
  31. if ($limit == $cnt) { //刷新一下輸出buffer,防止由于數據過多造成問題  
  32. ob_flush();  
  33. flush();  
  34. $cnt = 0;  
  35.  
  36. foreach ($row as $i => $v) {  
  37. $row[$i] = iconv('utf-8''gbk'$v);  
  38. }  
  39. fputcsv($fp$row);  

方法二,直接在瀏覽器用header輸出csv格式的數據,代碼如下:

  1. <?php  
  2. /*連接數據庫*/ 
  3. $DB_Server = "localhost"
  4. $DB_Username = "root";  
  5. $DB_Password = "123456";  
  6. $DB_DBName = "mydb";  //目標數據庫名 
  7. $DB_TBLName = "mytable";  //目標表名 
  8. $Connect = @mysql_connect($DB_Server$DB_Username$DB_Passwordor die("Couldn't connect.");  
  9. mysql_query("Set Names 'utf8'"); 
  10.  
  11. $savename = date("YmjHis"); //導出excel文件名 
  12. $file_type = "vnd.ms-excel";  
  13. $file_ending = "xls";  
  14. header("Content-Type: application/$file_type;charset=utf8");  
  15. header("Content-Disposition: attachment; filename=".$savename.".$file_ending");  
  16. //header("Pragma: no-cache"); 
  17.  
  18. /*寫入備注信息*/ 
  19. $now_date = date("Y-m-j H:i:s");  
  20. $title = "數據庫名:$DB_DBName,數據表:$DB_TBLName,備份日期:$now_date";  
  21. echo("$titlen"); 
  22.  
  23. /*查詢數據庫*/ 
  24. $sql = "Select * from $DB_TBLName";  
  25. $ALT_Db = @mysql_select_db($DB_DBName$Connector die("Couldn't select database");  
  26. $result = @mysql_query($sql,$Connector die(mysql_error()); 
  27.  
  28. /*寫入表字段名*/ 
  29. for ($i = 0; $i < mysql_num_fields($result); $i++) {  
  30.  echo mysql_field_name($result,$i) . ",";  
  31. }  
  32. echo "n"
  33.  
  34. /*寫入表數據*/ 
  35. $sep = ",t";  
  36. while($row = mysql_fetch_row($result)) {  
  37.  $data = "";  
  38.   for($i=0; $i<mysql_num_fields($result);$i++) {  
  39.    if(!isset($row[$i]))  
  40.     $data .= "NULL".$sep//處理NULL字段 
  41.    elseif ($row[$i] != "")  
  42.     $data .= "$row[$i]".$sep;  
  43.    else  
  44.     $data .= "".$sep//處理空字段 
  45.   }  
  46.  echo $data."n";  
  47. ?> 

例3,第二個差不多了,代碼如下:

  1. //搜索 
  2.     $start_time = strtotime($start_date); 
  3.     $end_time = strtotime($end_date); 
  4.     $sql = "select a.*,b.order_amount,b.money_paid from ".$ecs->table('invoice')." as a "
  5.                 " left join ".$ecs->table('order_info')." as b on a.order_id=b.order_sn"
  6.                 " where a.add_time >=".$start_time." and a.add_time <=".$end_time." "
  7.     $temp_list = $db->getAll($sql); 
  8.  
  9.     if($temp_list){//有數據 
  10.         $Html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><body>'.chr(13).chr(10); 
  11.         $Html.='<table width="700" border="1" align="center" cellpadding="2" cellspacing="1"
  12.                             <tr align="center"
  13.                                     <td align="center" nowrap="nowrap">時間:</td> 
  14.                                     <td align="center" nowrap="nowrap" colspan="9">'.$start_date.'~'.$end_date.'</td> 
  15.                             </tr> 
  16.                             <tr align="center"
  17.                                      <td align="center" nowrap="nowrap">編號</td> 
  18.                                      <td align="center" nowrap="nowrap">發票類型</td> 
  19.                                      <td align="center" nowrap="nowrap">發票抬頭</td> 
  20.                                      <td align="center" nowrap="nowrap">發票內容</td> 
  21.                                      <td align="center" nowrap="nowrap">訂單號</td> 
  22.                                      <td align="center" nowrap="nowrap">金額</td> 
  23.                                      <td align="center" nowrap="nowrap">添加日期</td> 
  24.                                      <td align="center" nowrap="nowrap">收件人</td> 
  25.                                      <td align="center" nowrap="nowrap">聯系方式</td> 
  26.                                      <td align="center" nowrap="nowrap">地址</td> 
  27.                            </tr>'; 
  28.             //取得符合條件的數組 
  29.             for($i=0;$i<count($temp_list);$i++){ 
  30.                  $temp_i = $i+1; 
  31.                  if($temp_list[$i][order_amount]==0){ 
  32.                      $temp_money = $temp_list[$i][money_paid]; 
  33.                  }else
  34.                      $temp_money = $temp_list[$i][order_amount]; 
  35.                  } 
  36.  
  37.                  $temp_time = date('Y-m-d'$temp_list[$i]['add_time']); 
  38.                  $Html.='<tr align="center"
  39.                                      <td align="center" nowrap="nowrap">'.$temp_i.'</td> 
  40.                                      <td align="center" nowrap="nowrap">'.$temp_list[$i][type_name].'</td> 
  41.                                      <td align="center" nowrap="nowrap">'.$temp_list[$i][top].'</td> 
  42.                                      <td align="center" nowrap="nowrap">'.$temp_list[$i][content].'</td> 
  43.                                      <td align="center" nowrap="nowrap" style="vnd.ms-excel.numberformat:@">'.$temp_list[$i][order_id].'</td> 
  44.                                      <td align="center" nowrap="nowrap">'.$temp_money.'</td> 
  45.                                      <td align="center" nowrap="nowrap">'.$temp_time.'</td> 
  46.                                      <td align="center" nowrap="nowrap">'.$temp_list[$i][user_name].'</td> 
  47.                                      <td align="center" nowrap="nowrap">'.$temp_list[$i][mobile].' '.$temp_list[$i][tel].' </td> 
  48.                                      <td align="center" nowrap="nowrap">'.$temp_list[$i][address].'</td> 
  49.                            </tr>'; 
  50.              } 
  51.              $Html.='</table>'
  52.              $Html.='</body></html>'
  53.              $mime_type = 'application/vnd.ms-excel'
  54.              header('Content-Type: ' . $mime_type); 
  55.              header('Content-Disposition: attachment; filename="invoice.xls"'); 
  56.              header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
  57.              header('Pragma: public'); 
  58.             echo $Html

有時excel會自動把數字轉換格式,于是有些手機號碼,身份證之類的就亂了,因此可以在導出時,先定義好如下代碼:

<td align="center" nowrap="nowrap" style="vnd.ms-excel.numberformat:@">'.$temp_list[$i][order_id].'</td>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岚皋县| 黔江区| 广安市| 龙里县| 侯马市| 塔城市| 天祝| 建宁县| 哈尔滨市| 宜宾县| 北宁市| 永丰县| 南通市| 墨玉县| 抚顺县| 泰来县| 舒兰市| 平阴县| 富锦市| 崇信县| 巴林左旗| 天气| 石狮市| 明溪县| 遂平县| 佛坪县| 无锡市| 青阳县| 大竹县| 宁南县| 南皮县| 海安县| 泽州县| 尼玛县| 镇坪县| 灌南县| 沾化县| 宜宾县| 美姑县| 大化| 锡林浩特市|