本文實(shí)例講述了php如何生成word并下載的具體實(shí)例。分享給大家供大家參考,具體如下:
先來(lái)看看前端的代碼 1.html
<!DOCTYPE html> <html> <head> <title>PHP生成Word文檔</title> <meta charset="utf-8"> </head> <body> <h1 style="text-align: center">xxx的簡(jiǎn)歷</h1> <h3>編號(hào):000001</h3> <table border="1" cellpadding="3" cellspacing="0" > <tr > <td width="93" valign="center" colspan="2" >姓名</td> <td width="160" valign="center" colspan="4" >xxx</td> <td width="89" valign="center" colspan="2" >學(xué)歷</td> <td width="156" valign="center" colspan="3" >xxx</td> <td width="125" colspan="2" rowspan="4" align="center" valign="middle" > <img src="./img.jpg" width="120" height="120" /> </td> </tr> <tr > <td width="93" valign="center" colspan="2" >性別</td> <td width="72" valign="center" colspan="2" >xxx</td> <td width="88" valign="center" colspan="2" >出生年月</td> <td width="89" valign="center" colspan="2" >xxx</td> <td width="68" valign="center" >戶籍地</td> <td width="87" valign="center" colspan="2" >xxx</td> </tr> <tr > <td width="93" valign="center" colspan="2" >身高</td> <td width="72" valign="center" colspan="2" >xxxcm</td> <td width="88" valign="center" colspan="2" >體重</td> <td width="89" valign="center" colspan="2" >xxxkg</td> <td width="68" valign="center" >婚姻狀況</td> <td width="87" valign="center" colspan="2" >xxx</td> </tr> <tr > <td width="93" valign="center" colspan="2" >手機(jī)</td> <td width="160" valign="center" colspan="4" >xxx</td> <td width="89" valign="center" colspan="2" >Email</td> <td width="156" valign="center" colspan="3" >xxx</td> </tr> <tr > <td width="93" valign="center" colspan="2" style="width:93px;">家庭住址</td> <td width="530" valign="center" colspan="11" >xxx</td> </tr> <tr > <td width="93" valign="center" colspan="2" rowspan="3">求職意向</td> <td width="93" valign="center" colspan="2">希望從事職業(yè)</td> <td width="200" valign="center" colspan="2">xxx</td> <td width="93" valign="center" colspan="2">希望薪資</td> <td width="200" valign="center" colspan="5">xxx元/月</td> </tr> <tr> <td width="93" valign="center" colspan="2" >希望工作地區(qū)</td> <td width="200" valign="center" colspan="2" >xxx</td> <td width="93" valign="center" colspan="2" >食宿要求</td> <td width="200" valign="center" colspan="5" >xxx</td> </tr> <tr> <td width="93" valign="center" colspan="2" >目前狀況</td> <td width="200" valign="center" colspan="9" >xxx</td> </tr> <tr> <td width="93" valign="center" style="color: red">自我評(píng)價(jià)</td> <td width="570" valign="center" colspan="12" >xxx</td> </tr> <tr> <td width="93" valign="center" >工作經(jīng)歷</td> <td width="570" valign="center" colspan="12" >xxx</td> </tr> <tr> <td width="93" valign="center" >教育經(jīng)歷</td> <td width="570" valign="center" colspan="12" >xxx</td> </tr> <tr> <td width="93" valign="center" >培訓(xùn)經(jīng)歷</td> <td width="570" valign="center" colspan="12" >xxx</td> </tr> </table> <p><a href="./1.php" rel="external nofollow" >點(diǎn)擊下載</a></p> </body> </html>
后端PHP實(shí)現(xiàn)的代碼,很簡(jiǎn)單
<?php //獲取1.html文檔的內(nèi)容(包括html代碼) $result = file_get_contents('./1.html'); echo "$result"; // /////////////////////保存/////////////////////////// //打開緩沖區(qū) ob_start(); header("Cache-Control: public"); Header("Content-type: Application/octet-stream"); Header("Accept-Ranges: bytes"); //判斷瀏覽器類型 if (strpos($_SERVER["HTTP_USER_AGENT"],'MSIE')) { header('Content-Disposition: attachment; filename=test.doc'); }else if (strpos($_SERVER["HTTP_USER_AGENT"],'Firefox')) { Header('Content-Disposition: attachment; filename=test.doc'); } else { header('Content-Disposition: attachment; filename=test.doc'); } //不使用緩存 header("Pragma:no-cache"); //過期時(shí)間 header("Expires:0"); //輸出全部?jī)?nèi)容到瀏覽器 ob_end_flush(); ?>
網(wǎng)頁(yè)運(yùn)行結(jié)果:
點(diǎn)擊下載后生成的word文檔如下截圖,自己可以試試吧
原理很簡(jiǎn)單
首先獲取到要下載的前端頁(yè)面的html代碼(file_get_contents方法),然后對(duì)文檔流進(jìn)行相關(guān)配置,最后輸出即可。