龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php 保存数据库查询结果到Excel

时间:2014-07-15 02:09来源:网络整理 作者:网络 点击:
分享到:
保存数据库查询结果到Excel [代码片段(67行)]
excel.php
<?php
class excel {
    function start() {
        ob_start ();
    }
    function save($path) {
        $data = ob_get_contents ();
        ob_end_clean ();
        $this->wirtetoexcel ( $path, $data );
    }
    function wirtetoexcel($fn, $data) {
        $fp = fopen ( $fn, "wb" );
        fwrite ( $fp, $data );
        fclose ( $fp );
    }
}
?>

查询数据库并保存为excel
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="css/style.css" rel="stylesheet" type="text/css" />
<?php
include ("conn.php");
$query = mysql_query ( "select * from map order by register_date desc" );
$i = $perpagenum * ($page - 1) + 1;
include_once ("excel.php");
$Excel = new Excel ();
$Excel->start ();
?>
<table width="600" border="0">
    <tr>
        <td align="center">companyname_cn</td>
        <td align="center">companyname_en"</td>
        <td align="center">name</td>
        <td align="center">position</td>
        <td align="center">tel</td>
        <td align="center">fax</td>
        <td align="center">email</td>
        <td align="center">website</td>
        <td align="center">product</td>
    </tr>
<?php
while ( $myrow = mysql_fetch_array ( $query ) ) {
    ?>
<tr>
        <td align="center"><?php echo $myrow["companyname_cn"]; ?></td>
        <td align="center"><?php echo $myrow["companyname_en"]; ?></td>
        <td align="center"><?php echo $myrow["name"]; ?></td>
        <td align="center"><?php echo $myrow["position"]; ?></td>
        <td align="center"><?php echo $myrow["tel"]; ?></td>
        <td align="center"><?php echo $myrow["fax"]; ?></td>
        <td align="center"><?php echo $myrow["email"]; ?></td>
        <td align="center"><?php echo $myrow["website"]; ?></td>
        <td align="center"><?php echo $myrow["product"]; ?></td>
    </tr>
<?

}
?>
</table>
<p>
<?php
$Excel->save("Excel/data.xls");
?>
</p>
//该片段来自于http://outofmemory.cn
精彩图集

赞助商链接