关于纯HTML格式写入word
生活随笔
收集整理的這篇文章主要介紹了
关于纯HTML格式写入word
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用mht格式生成的word文檔不適合批量導出,用純HTML生成的word文件可以批量導出。我不適用框架直接用localhost本地執行PHP文件的格式如下:
<?php header("content-type:text/html;charset=utf-8"); class word{function start(){ob_start();echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:w="urn:schemas-microsoft-com:office:word"xmlns="http://www.w3.org/TR/REC-html40">';}function save($path){ echo "</html>";$data = ob_get_contents();ob_end_clean(); $this->wirtefile ($path,$data);}function wirtefile ($fn,$data){$fp=fopen($fn,"wb");fwrite($fp,$data);fclose($fp);} } $basic_code = '001'; $sce_name = '大明湖'; $sce_status = '已開發'; $area_name = '山東省濟南市歷下區'; $sce_addr = '山東省濟南市歷下區'; $sce_coord = '117.03246,36.680728'; $sce_form = '兩面荷花三面柳,半城山色半城湖'; $now_status = '收費+免費'; $html = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <xml><w:WordDocument><w:View>Print</w:View></xml> </head>'; $html .= '<body> <h1 style="text-align:center">旅游資源普查表</h1> <div> <span>代碼:'.$basic_code.'</span> <span>序號:</span> </div> <table border="1" cellpadding="3" cellspacing="0"> <tr><td width="100">名 稱:</td><td width="700" colspan="12">'.$sce_name.'</td> </tr> <tr><td width="100">開發狀況:</td><td width="700" colspan="12">'.$sce_status.'</td> </tr> <tr><td width="100">所屬管轄:</td><td width="700" colspan="12">'.$area_name.'</td> </tr> <tr><td width="100">位 置:</td><td width="700" colspan="12">'.$sce_addr.'</td> </tr> <tr><td width="100">地理坐標:</td><td width="700" colspan="12">'.$sce_coord.'</td> </tr> <tr><td width="100">基本特征:</td><td width="700" colspan="12"> '.$sce_form.'</td> </tr> <tr><td width="100">保護與開發現狀:</td><td width="700" colspan="12">'.$now_status.'</td> </tr>'; // foreach($img_src as $v){// $html .= '<tr>// <td width="100" valign="center" >圖片顯示</td>// <td width="700" valign="center" colspan="12" ><img src="'.$v.'" width="400" /></td>// </tr>'; // } $html .= '</table></body>'; //批量生成(自己修改) for($i=1;$i<=3;$i++){ $word = new word(); $word->start(); //$html = "aaa".$i; $wordname = 'test'.$i.".doc"; echo $html; $word->save($wordname); ob_flush();//每次執行前刷新緩存 flush(); }注意:該php文件只在本地(服務器)生成word文檔,沒有寫下載功能。如有需要請參考《生成mht格式導出word》那篇文檔。
總結
以上是生活随笔為你收集整理的关于纯HTML格式写入word的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用专用文件夹隐藏文件 (tasks、f
- 下一篇: 解决ubuntu系统root用户下Chr