实例详解PHP中html word 互转的方法
下面一段代码给大家介绍php将html转入word中,具体内容如下所示:
这是经过测试的,这种方法有一点不好,html页面代码要写在php中,不过好歹能运行,看程序
<?php classword{ functionstart(){ ob_start(); echo'<htmlxmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">'; } functionsave($path) { echo"</html>"; $data=ob_get_contents(); } functionwirtefile($fn,$data){ $fp=fopen($fp,$data); fwrite($fp,$data); } } $html=' <tablewidth=600cellpadding="6"cellspacing="1"style="border:1pxsolidgreen;"style="border-collapse:collapse"> <trstyle="border:1pxsolidgreen;"> <tdstyle="border:1pxsolidgreen;">姓名</td> <tdstyle="border:1pxsolidgreen;">性别</td> <tdstyle="border:1pxsolidgreen;">年龄</td> <tdstyle="border:1pxsolidgreen;">爱好</td> <tdstyle="border:1pxsolidgreen;">备注</td> </tr> <trstyle="border:1pxsolidgreen;"> <tdstyle="border:1pxsolidgreen;">张三</td> <tdstyle="border:1pxsolidgreen;">男</td> <tdstyle="border:1pxsolidgreen;">32</td> <tdstyle="border:1pxsolidgreen;">足球</td> <tdstyle="border:1pxsolidgreen;">无</td> </tr> <trstyle="border:1pxsolidgreen;"> <tdstyle="border:1pxsolidgreen;"> 李四 </td> <tdstyle="border:1pxsolidgreen;">男</td><tdstyle="border:1pxsolidgreen;">43</td><tdstyle="border:1pxsolidgreen;">篮球</td><tdstyle="border:1pxsolidgreen;">无</td> </tr> </table> '; //批量生成 //for($i=1;$i<3;$i++){ $word=newword(); $word->start(); $wordname='个人测试php生成word.doc'; echo$html; $word->save($wordname); header('Content-type:application/word'); header('Content-Disposition:attachment;filename='.$wordname.''); //readfile($wordname); ob_flush();//每次执行前刷新缓存 flush(); //} ?>
下面给大家分享一段代码php实现word转html的方法
要想完美解决,office转pdf或者html,最好还是用windowsoffice软件,libreoffice不能完美转换,wps没有api。
先确认com模块是不是开启,phpinfo里面如果有com_dotnet模块,说明已开启,如果没有,修改php.ini,
代码如下:
com.allow_dcom=true
前面的注释去掉,重启就OK了,php官方网站说,php5.4.5之前,com模块是内置的,其实也不一定全是,官网下的php5.3.39,com模块就没有内置。
如果不是内置模块的话,php.ini加上,前提你的ext文件夹下,有该扩展
代码如下:
extension=php_com_dotnet.dll
然后重启就OK了
functionword2html($wordname,$htmlname) { $word=newCOM("word.application")ordie("UnabletoinstanciateWord"); $word->Visible=1; $word->Documents->Open($wordname); $word->Documents[1]->SaveAs($htmlname,8); $word->Quit(); $word=null; unset($word); } word2html('D:/www/test/6.docx','D:/www/test/6.html');
注意:
1.转换出来的html,查看源码,比较乱的
2.转换过程中会调用winword.exe
3.如果页面一直在加载,把文档重命名,然后在重新转。