public function doc_testOp() { //$data = $this->db->where(array('id'=>1))->find();//從數據庫查詢一條數據 //$this->assign($data);//把獲取的數據傳遞的模板,替換模板裏面的變量 //$content = $this->fetch('word');//獲取模板內容信息word是模板的名稱 $content = '<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>報名表測試</title> <style> @font-face { font-family:"Times New Roman"; } @font-face { font-family:"宋体"; } @font-face { font-family:"Arial"; } table{border-collapse:collapse;border-color:#000;} td{ border-color:#000; padding:10px 5px; vertical-align:middle;} h1{ text-align:center} h3{ text-align:right;} </style> <!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val="Cambria Math"/><m:brkBin m:val="before"/><m:brkBinSub m:val="--"/><m:smallFrac m:val="off"/><m:dispDef/><m:lMargin m:val="0"/> <m:rMargin m:val="0"/><m:defJc m:val="centerGroup"/><m:wrapIndent m:val="1440"/><m:intLim m:val="subSup"/><m:naryLim m:val="undOvr"/></m:mathPr></w:WordDocument></xml><![endif]--> </head> <body> <h1>報名表測試</h1> <h3>編號:{$number}</h3> <table border="1" cellpadding="3" cellspacing="0" > <tr > <td width="93" valign="center" colspan="2" >姓名</td> <td width="160" valign="center" colspan="4" >{$username}</td> <td width="89" valign="center" colspan="2" >報考專業</td> <td width="156" valign="center" colspan="3" >{$Zhiyuan[$zyid][\'name\']}</td> <td width="125" colspan="2" rowspan="4" align="center" valign="middle" ><img src="{$Config.siteurl}upload/bm_avatar/{$id}/180x180.jpg" width="120" height="120" /></td> </tr> <tr > <td width="93" valign="center" colspan="2" >性別</td> <td width="72" valign="center" colspan="2" >{:Sex($sex)}</td> <td width="88" valign="center" colspan="2" >出生年月</td> <td width="89" valign="center" colspan="2" >{$birthday}</td> <td width="68" valign="center" >民族</td> <td width="87" valign="center" colspan="2" >{$ethnic}</td> </tr> <tr > <td width="93" valign="center" colspan="2" >身高</td> <td width="72" valign="center" colspan="2" >{$height}</td> <td width="88" valign="center" colspan="2" >體重</td> <td width="89" valign="center" colspan="2" >{$weight}</td> <td width="68" valign="center" >視力</td> <td width="87" valign="center" colspan="2" >{$vision}</td> </tr> <tr > <td width="93" valign="center" colspan="2" >手機</td> <td width="160" valign="center" colspan="4" >{$mobile}</td> <td width="89" valign="center" colspan="2" >固定電話</td> <td width="156" valign="center" colspan="3" >{$tel}</td> </tr> <tr > <td width="93" valign="center" colspan="2" style="width:93px;">家庭住址</td> <td width="530" valign="center" colspan="11" >{$address}</td> </tr> <tr > <td width="53" valign="center" rowspan="2" >畢業學校</td> <td width="570" valign="center" colspan="12" ><strong>小學:</strong>{$xschool}</td> </tr> <tr > <td width="570" valign="center" colspan="12" ><strong>中學:</strong>{$zschool}</td> </tr> <tr > <td width="53" valign="center" rowspan="3" >父母或監護人狀況</td> <td width="81" valign="center" colspan="2" >姓 名</td> <td width="52" valign="center" colspan="2" >籍貫</td> <td width="104" valign="center" colspan="2" >身高</td> <td width="127" valign="center" colspan="3" >工做單位</td> <td width="90" valign="center" colspan="2" >學歷</td> <td width="114" valign="center" >電 話</td> </tr> <tr > <td width="81" valign="center" colspan="2" >{$father[0]}</td> <td width="52" valign="center" colspan="2" >{$father[5]}</td> <td width="104" valign="center" colspan="2" >{$father[1]}</td> <td width="127" valign="center" colspan="3" >{$father[3]}</td> <td width="90" valign="center" colspan="2" >{$father[2]}</td> <td width="114" valign="center" >{$father[4]}</td> </tr> <tr > <td width="81" valign="center" colspan="2" >{$mother[0]}</td> <td width="52" valign="center" colspan="2" >{$mother[5]}</td> <td width="104" valign="center" colspan="2" >{$mother[1]}</td> <td width="127" valign="center" colspan="3" >{$mother[3]}</td> <td width="90" valign="center" colspan="2" >{$mother[2]}</td> <td width="114" valign="center" >{$mother[4]}</td> </tr> <tr > <td width="53" valign="center" rowspan="3" >父母或監護人狀況</td> <td width="81" valign="center" colspan="2" >姓 名</td> <td width="52" valign="center" colspan="2" >籍貫</td> <td width="104" valign="center" colspan="2" >身高</td> <td width="127" valign="center" colspan="3" >工做單位</td> <td width="90" valign="center" colspan="2" >學歷</td> <td width="114" valign="center" >電 話</td> </tr> <tr > <td width="81" valign="center" colspan="2" >{$father[0]}</td> <td width="52" valign="center" colspan="2" >{$father[5]}</td> <td width="104" valign="center" colspan="2" >{$father[1]}</td> <td width="127" valign="center" colspan="3" >{$father[3]}</td> <td width="90" valign="center" colspan="2" >{$father[2]}</td> <td width="114" valign="center" >{$father[4]}</td> </tr> <tr > <td width="81" valign="center" colspan="2" >{$mother[0]}</td> <td width="52" valign="center" colspan="2" >{$mother[5]}</td> <td width="104" valign="center" colspan="2" >{$mother[1]}</td> <td width="127" valign="center" colspan="3" >{$mother[3]}</td> <td width="90" valign="center" colspan="2" >{$mother[2]}</td> <td width="114" valign="center" >{$mother[4]}</td> </tr> <tr > <td width="53" valign="center" rowspan="3" >父母或監護人狀況</td> <td width="81" valign="center" colspan="2" >姓 名</td> <td width="52" valign="center" colspan="2" >籍貫</td> <td width="104" valign="center" colspan="2" >身高</td> <td width="127" valign="center" colspan="3" >工做單位</td> <td width="90" valign="center" colspan="2" >學歷</td> <td width="114" valign="center" >電 話</td> </tr> <tr > <td width="81" valign="center" colspan="2" >{$father[0]}</td> <td width="52" valign="center" colspan="2" >{$father[5]}</td> <td width="104" valign="center" colspan="2" >{$father[1]}</td> <td width="127" valign="center" colspan="3" >{$father[3]}</td> <td width="90" valign="center" colspan="2" >{$father[2]}</td> <td width="114" valign="center" >{$father[4]}</td> </tr> <tr > <td width="81" valign="center" colspan="2" >{$mother[0]}</td> <td width="52" valign="center" colspan="2" >{$mother[5]}</td> <td width="104" valign="center" colspan="2" >{$mother[1]}</td> <td width="127" valign="center" colspan="3" >{$mother[3]}</td> <td width="90" valign="center" colspan="2" >{$mother[2]}</td> <td width="114" valign="center" >{$mother[4]}</td> </tr> <tr > <td width="53" valign="center" >學習培訓簡歷</td> <td width="570" valign="center" colspan="12" >{$training}</td> </tr> <tr > <td width="53" valign="center" >專項 成績 獲獎 狀況</td> <td width="570" valign="center" colspan="12" >{$training}</td> </tr> </table> </body> </html>'; $fileContent =$this->WordMake($content);//生成word內容 $name = iconv("utf-8", "GBK",'測試文檔');//轉換好生成的word文件名編碼 //$fp = fopen($name.".doc", 'w');//打開生成的文檔 //fwrite($fp, $fileContent);//寫入包保存文件 //header($fp); @header("Content-Type: application/doc"); @header("Content-Disposition: attachment; filename=" . $name . ".doc"); echo stripslashes($fileContent); } /** * 根據HTML代碼獲取word文檔內容 * 建立一個本質爲mht的文檔,該函數會分析文件內容並從遠程下載頁面中的圖片資源 * 該函數依賴於類WordMake * 該函數會分析img標籤,提取src的屬性值。可是,src的屬性值必須被引號包圍,不然不能提取 * * @param string $content HTML內容 * @param string $absolutePath 網頁的絕對路徑。若是HTML內容裏的圖片路徑爲相對路徑,那麼就須要填寫這個參數,來讓該函數自動填補成絕對路徑。這個參數最後須要以/結束 * @param bool $isEraseLink 是否去掉HTML內容中的連接 */ function WordMake( $content , $absolutePath = "" , $isEraseLink = true ) { //import("Wordmaker.class"); require_once BASE_DATA_PATH.'/vendors/doc/Wordmaker.class.php'; $mht = new Wordmaker(); if ($isEraseLink){ $content = preg_replace('/<a\s*.*?\s*>(\s*.*?\s*)<\/a>/i' , '$1' , $content); //去掉連接 } $images = array(); $files = array(); $matches = array(); //這個算法要求src後的屬性值必須使用引號括起來 if ( preg_match_all('/<img[.\n]*?src\s*?=\s*?[\"\'](.*?)[\"\'](.*?)\/>/i',$content ,$matches ) ){ $arrPath = $matches[1]; for ( $i=0;$i<count($arrPath);$i++) { $path = $arrPath[$i]; $imgPath = trim( $path ); if ( $imgPath != "" ) { $files[] = $imgPath; if( substr($imgPath,0,7) == 'http://') { //絕對連接,不加前綴 } else { $imgPath = $absolutePath.$imgPath; } $images[] = $imgPath; } } } $mht->AddContents("tmp.html",$mht->GetMimeType("tmp.html"),$content); for ( $i=0;$i<count($images);$i++) { $image = $images[$i]; if ( @fopen($image , 'r') ) { $imgcontent = @file_get_contents( $image ); if ( $content ) $mht->AddContents($files[$i],$mht->GetMimeType($image),$imgcontent); } else { echo "file:".$image." not exist!<br />"; } } return $mht->GetFile(); }
效果圖就是這樣的:php
只是測試一下 - - 在網上看到的,上面是我本身隨便寫的測試代碼 ,須要優化的。html
原文地址在這裏:算法
https://www.moretouch.com.cn/news/jishufenxiang/31.html數據庫
須要的能夠本身好好研究下。app