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(); }
效果图就是这样的:
只是测试一下 - - 在网上看到的,上面是我自己随便写的测试代码 ,需要优化的。
原文地址在这里:
https://www.moretouch.com.cn/news/jishufenxiang/31.html
需要的可以自己好好研究下。