PHP中匯出Excel,將資料以Excel形式匯出

來源:互聯網
上載者:User

標籤:

現在,很多地方都需要匯出資料,這裡說一種簡單的方法將資料以Excel的形式匯出,方法如下:

 1 <?php  2     date_default_timezone_set(‘PRC‘);//設定時區 3      4     /*設定head頭資訊*/ 5     Header("Content-Type:application/vnd.ms-excel;charset=UTF-8"); 6     Header("Accept-Ranges:bytes"); 7     Header("Content-Disposition:attachment;filename=".date(‘YmdHis‘).".xls"); 8     Header("Pragma:no-cache"); 9     Header("Expires:0");10     11     $str = $str2 = null;12     /*設定表格資訊*/13     $str.= "ID"."\t";14     $str.= "使用者名稱"."\t";15     $str.= "密碼"."\t";16     $str.= "郵箱"."\t\r\n";17     $info = iconv("UTF-8","GBK",$str);18     echo $info;19     20     /*查詢內容匯出資料庫資料*/21     $link = @mysql_connect(‘localhost‘,‘root‘,‘‘) or die(‘Failed to connect to the MySQL‘); 22     mysql_select_db(‘l_test‘,$link);23     mysql_set_charset(‘utf8‘,$link);24     $query = ‘select * from lt_user‘;25     $result = mysql_query($query);26     while($row = mysql_fetch_assoc($result)){27         $list[] = $row; 28     }29     mysql_free_result($result);30     mysql_close($link);31     32     foreach($list as $v){33         $str2.= trim(iconv("UTF-8","GBK",$v[‘id‘]))."\t";34         $str2.= trim(iconv("UTF-8","GBK",$v[‘username‘]))."\t";35         $str2.= trim(iconv("UTF-8","GBK",$v[‘passwd‘]))."\t";36         $str2.= trim(iconv("UTF-8","GBK",$v[‘email‘]))."\t\r\n";37     }38     echo $str2;39 ?> 

這個方法相對簡單,但在一些小的程式上,作為一個簡單的方法還是不錯的,希望能對大家有所協助。

PHP中匯出Excel,將資料以Excel形式匯出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.