日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

PHP導出Mysql數據到Excel

系統 2169 0

臨時需要將Mysql中一張表導出成Excel表格,有個phpexcel的插件可以用,我覺得有點麻煩,況且我是臨時要備份的,就直接自己寫了。

      <?
      
        php 


      
      
        /*
      
      
        連接數據庫
      
      
        */
      
      
        $DB_Server
      
       = "ServerIP"
      
        ;

 
      
      
        $DB_Username
      
       = "UserName"
      
        ; 

 
      
      
        $DB_Password
      
       = "PassWord"
      
        ; 

 
      
      
        $DB_DBName
      
       = "DBname";  
      
        //
      
      
        目標數據庫名
      
      
        $DB_TBLName
      
       = "TableName";  
      
        //
      
      
        目標表名
      
      
        $Connect
      
       = @
      
        mysql_connect
      
      (
      
        $DB_Server
      
      , 
      
        $DB_Username
      
      , 
      
        $DB_Password
      
      ) or 
      
        die
      
      ("Couldn't connect."
      
        ); 

 
      
      
        mysql_query
      
      ("set names utf8"
      
        ); 




      
      
        $savename
      
       = 
      
        date
      
      ("YmjHis"); 
      
        //
      
      
        導出excel文件名
      
      
        $file_type
      
       = "vnd.ms-excel"
      
        ; 

 
      
      
        $file_ending
      
       = "xls"
      
        ; 

 
      
      
        header
      
      ("Content-Type: application/
      
        $file_type
      
      ;charset=utf-8"
      
        ); 

 
      
      
        header
      
      ("Content-Disposition: attachment; filename=".
      
        $savename
      
      .".
      
        $file_ending
      
      "
      
        ); 


      
      
        header
      
      ("Pragma: no-cache"
      
        ); 




      
      
        /*
      
      
        寫入備注信息
      
      
        */
      
      
        $now_date
      
       = 
      
        date
      
      ("Y-m-j H:i:s"
      
        ); 

 
      
      
        $title
      
       = "數據庫名:
      
        $DB_DBName
      
      ,數據表:
      
        $DB_TBLName
      
      ,備份日期:
      
        $now_date
      
      "
      
        ; 

 
      
      
        echo
      
      
        iconv
      
      ("utf-8","gbk",
      
        $title
      
      )."\n"
      
        ; 




      
      
        /*
      
      
        查詢數據庫
      
      
        */
      
      
        $sql
      
       = "Select * from 
      
        $DB_TBLName
      
      "
      
        ; 

 
      
      
        $ALT_Db
      
       = @
      
        mysql_select_db
      
      (
      
        $DB_DBName
      
      , 
      
        $Connect
      
      ) or 
      
        die
      
      ("Couldn't select database"
      
        ); 

 
      
      
        $result
      
       = @
      
        mysql_query
      
      (
      
        $sql
      
      ,
      
        $Connect
      
      ) or 
      
        die
      
      (
      
        mysql_error
      
      
        ()); 




      
      
        /*
      
      
        寫入表字段名
      
      
        */
      
      
        for
      
       (
      
        $i
      
       = 0; 
      
        $i
      
       < 
      
        mysql_num_fields
      
      (
      
        $result
      
      ); 
      
        $i
      
      ++
      
        ) { 

  

  
      
      
        echo
      
      
        mysql_field_name
      
      (
      
        $result
      
      ,
      
        $i
      
      ) . "\t"
      
        ; 

 } 

 
      
      
        echo
      
       "\n"
      
        ;




      
      
        /*
      
      
        寫入表數據
      
      
        */
      
      
        $sep
      
       = "\t"
      
        ; 

 
      
      
        while
      
      (
      
        $row
      
       = 
      
        mysql_fetch_row
      
      (
      
        $result
      
      
        )) { 

  
      
      
        $data
      
       = ""
      
        ; 

   
      
      
        for
      
      (
      
        $i
      
      =0; 
      
        $i
      
      <
      
        mysql_num_fields
      
      (
      
        $result
      
      );
      
        $i
      
      ++
      
        ) { 

    
      
      
        if
      
      (!
      
        isset
      
      (
      
        $row
      
      [
      
        $i
      
      
        ])) 

     
      
      
        $data
      
       .= "NULL".
      
        $sep
      
      ; 
      
        //
      
      
        處理NULL字段
      
      
        elseif
      
       (
      
        $row
      
      [
      
        $i
      
      ] != ""
      
        ){

     
      
      
        $datmp
      
      =
      
        iconv
      
      ("utf-8", "gbk",
      
        $row
      
      [
      
        $i
      
      
        ]);

     
      
      
        $data
      
       .= 
      
        $datmp
      
      .
      
        $sep
      
      
        ; 

   }

    
      
      
        else
      
      
        $data
      
       .= "".
      
        $sep
      
      ; 
      
        //
      
      
        處理空字段
      
      
          } 

  
      
      
        echo
      
      
        $data
      
      ."\n"
      
        ; 

 }

 
      
      ?> 
    

好了,這樣直接訪問這個php文件就可以將指定的表中數據導出了。

PHP導出Mysql數據到Excel


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 平塘县| 钦州市| 沙河市| 通州区| 漳浦县| 曲阳县| 崇文区| 安陆市| 武清区| 南岸区| 海晏县| 招远市| 喀什市| 西平县| 疏附县| 兴安盟| 合阳县| 永丰县| 美姑县| 临洮县| 萝北县| 滕州市| 丽江市| 秦安县| 乡宁县| 锦州市| 定兴县| 东光县| 新营市| 台中市| 涿州市| 盐津县| 石城县| 车致| 韶山市| 新野县| 敦化市| 景谷| 松江区| 巴里| 同心县|