一個php文件就能夠把數據庫的數據導出Excel表格

數據庫內容太多,複製粘貼太麻煩?
那就用代碼實現把,把代碼寫好了,導出還不容易嗎,訪問便可導出。php

excel.phphtml

<?php
    error_reporting(E_ALL ^ E_DEPRECATED);
    $localhost = '數據庫地址';
    $dbname = '數據庫名';
    $dbuser = '數據庫帳號';
    $dbpwd = '數據庫密碼';
    $tbname = "須要導出的數據庫表名";

    ob_end_clean();
    header('Content-type: text/html; charset=utf-8');
    header("Content-type:application/vnd.ms-excel;charset=UTF-8"); 
    header("Content-Disposition:filename=data.xls");// 文件名本身改,默認data.xls

    $conn = mysql_connect($localhost,$dbuser,$dbpwd) or die("鏈接數據庫失敗");
    mysql_select_db($dbname, $conn);
    mysql_query("set names 'UTF-8'");

    $result=mysql_query("SELECT * FROM $tbname");
    echo "<table>";
    echo "<tr>";
    echo "<th>A1單元格</th>";
    echo "<th>B1單元格</th>";
    echo "<th>C1單元格</th>";
    echo "<th>D1單元格</th>";
    echo "</tr>";
    echo "</table>";
    while($row=mysql_fetch_array($result)){
        //下面data改爲你本身數據庫對應的字段
        $data1 = $row["data1"];
        $data2 = $row["data2"];
        $data3 = $row["data3"];
        $data4 = $row["data4"];
        
        //輸出字段的數據到單元格
        //下面data改爲你本身數據庫對應的字段
        echo "<table>";
        echo "<tr>";
            echo "<td>$data1</td>";
            echo "<td>$data2</td>";
            echo "<td>$data3</td>";
            echo "<td>$data4</td>";
        echo "</tr>";
        echo "</table>";
    }
?>

而後訪問excel.php就能夠彈出表格下載了。
做者:TANKING
網站:likeyunba.com
微信:likeyunba520mysql

相關文章
相關標籤/搜索