本文主要和大家分享 php使用com组件转换word文档为html并实现在线预览,希望能帮助到大家。
PHP5.4
<span style="font-size: 14px;">[CoM];<br>path to a file containing GUlDs,llDs or filenames of files with TypeLibs;<br>http://php.net/com.typelib-file;com.typelib_file=<br><br>;allow Distributed-COM calls<br>;http://php.cn/com.allow-dcomcom.allow dcom= true<br></span>
PHP7.0
在php.ini中增加
<span style="font-size: 14px;">[COM_DOT_NET]extension=php_com_dotnet.dll<br></span>
重启 php即可.
在根目录输入
<span style="font-size: 14px;"><?php <br>error_reporting(E_ALL);<br>ini_set("display_errors","On");<br>$excel = new COM("Excel.application") or die ("ERROR: Unable to instantaniate COM!
");<br><br>print_r(get_loaded_extensions());?><br></span>
可以数组形式罗列php的现有组件.
执行程序
新建index.php, 默认传入3.doc , 然后运行就可以查看效果了.
<span style="font-size: 14px;"><?php /**<br> * Created by PhpStorm.<br> * User: zhangcanlong<br> * Date: 2016/11/15<br> * Time: 23:41<br> */function word2html($wordname,$htmlname){<br> //获取链接地址<br> /*<br> //$url=$_SERVER['HTTP_HOST'];//获取服务器地址<br> // $url=$url.$_SERVER['PHP_SELF'];//获取当前服务器下的文件名和目录<br> // $url=dirname($url)."/";<br> */ <br> //去除目录中的文件名<br> $word = new COM("word.application") or die("找不到 Word 程序"); // 建立一个指向新COM组件的索引<br> // 显示目前正在使用的Word的版本号<br> //echo "Loading Word, v. {$word->Version}<br>";<br> // 把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真)<br> $word->Visible = 0; <br> $word->Documents->Open($wordname) or die("无法打开这文件");<br><br> header("Content-Type: text/html;charset=gb2312");//设置文件的格式<br> //打开一个文档<br> //把文档保存在目录中<br> try{ <br> $word->Documents[1]->SaveAs($htmlname,8);<br> } catch(Exception $e){ <br> print $e->getMessage();<br> } <br> $content=file_get_contents($htmlname); <br> echo $content;//输出word文档的内容<br> // 关闭与COM组件之间的连接<br> $word->Quit(); <br> unset($word);<br>}<br>$fileName = '3.doc';<br>$wordName = explode('.',$fileName)[0];$wordExt = explode('.',$fileName)[1];//获取当前文件下的目录<br>$file_Name=dirname(__FILE__);<br>if (is_file($wordName.'.html')) { <br>echo file_get_contents($wordName.'.html');<br>}else{<br> word2html("$file_Name\".$wordName.".".$wordExt,"$file_Name\".$wordName.".html");//要转换的word文件和转换成的html的文件名}<br></span>
可以看到当前目录的1.docx会生成1.html文件,另外网站展示1.html
相关推荐:
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











