標籤: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中。