php輸出excel的簡單方法(csv格式)

來源:互聯網
上載者:User

如果不讀資料我們直接使用下面代碼就可以了,只需要把相關的資料匯出到excel表就可以了,這麼簡單的操作就不需要用那些類庫什麼的了。直接用header的方式就可以了:header("Content-type:application/vnd.ms-excel");

 代碼如下 複製代碼

<?php
header(“Content-type:application/vnd.ms-excel”);
header(‘Content-Disposition: attachment;filename=”yingcai.xls”‘);

?>
<table border=”1″>
<tr>
<td>使用者名稱</td>
<td>密碼</td>
</tr>
<tr>
<td>admin</td>
<td>yingcai</td>
</tr>
</table>


簡單純粹的php匯出excel表格方法,木有過多的解釋,花哨的裝飾,一看就懂

 代碼如下 複製代碼


header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment; filename=使用者資訊表.xls");
require_once('./class.db.php');
$db = new DB('localhost', 'app_vmovies', 'root', '123456');
$sql = 'select * from `user` order by `id` desc';
$ret = $db->get_results($sql);
 
// 輸出內容如下:   
echo "ID"."t";
echo "暱稱"."t";
echo "性別"."t";
echo "OpenId"."t";
echo "平台"."t";
echo "註冊日期"."t";
echo "n";
 
$len = count($ret);
for ( $i = 0; $i < $len; $i++ ) {
    echo $ret[$i]["id"]."t";
    echo $ret[$i]["nickname"]."t";
    echo $ret[$i]["gender"]."t";
    echo $ret[$i]["openid"]."t";
    echo $ret[$i]["pf"]."t";
    echo $ret[$i]["regdate"]."t";
    echo "n";
}

如果程式是utf-8編碼的,還需要用iconv函數去轉碼,不然是會亂碼的,亂碼的。

另word格式匯入類似,,指定header就可以了:

 代碼如下 複製代碼

header("Content-Type:   application/msword");       
header("Content-Disposition:   attachment;   filename=doc.doc"); 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.