PHP操作:將資料庫中的資料儲存到Word中。

來源:互聯網
上載者:User

標籤:sub   order   清空   lan   ati   utf-8   調用   div   button   

1.首先定義了一個word類

<?phpclass word{    function start()    {      ob_start();        ob_start — 開啟輸出控制緩衝     }    function save($path)      {        $data = ob_get_contents();     ob_get_contents — 返回輸出緩衝區的內容        ob_end_clean();             ob_end_clean — 清空(擦除)緩衝區並關閉輸出緩衝        $this->wirtetoword($path,$data);     }   function wirtetoword ($fn,$data)     {       $fp=fopen($fn,"w");  fopen — 開啟檔案或者 URL,第二個參數是表示以什麼方式開啟       fwrite($fp,$data);        fwrite — 寫入檔案(可安全用於二進位檔案)       fclose($fp);      fclose — 關閉一個已開啟的檔案指標    }}?>

2.引用該類,並從資料庫中調用nation表的資料

<!doctype html><html lang="en"><head>    <meta charset="UTF-8" />    <title>Document</title></head><body>    <?php        if(!isset($_GET["id"]))        {            ?>            <input type="button" name="submit" value="將表格內容儲存到Word" onclick="window.location.href=‘main.php?id=print‘">        <?php}        ?>    <?php    if($_GET["id"]!="")    {        include("word.class.php");  將word類引用進來            $word=new word();   對該類進行執行個體化            $word->start();      調用start()方法,定義要儲存表格的開始                    }                        ?>                <table border="1" cellspacing="0" cellpadding="0">            <tr><th>代號</th><th>名稱</th></tr>            <?php                include("../gongju/DBDA.class.php");                $db=new DBDA();                $sql="select * from nation";                $attr=$db->Query($sql);                foreach($attr as $v)                {                    echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td></tr>";                }                                ?>        </table>        <?php        if($_GET["id"]!=""){            $word->save("data.doc");    儲存表格結束        }            ?>                    </body></html>

 

PHP操作:將資料庫中的資料儲存到Word中。

相關文章

聯繫我們

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