首页  >  文章  >  后端开发  >  PHP实现转换word为HTML并在线预览

PHP实现转换word为HTML并在线预览

小云云
小云云原创
2018-05-28 16:46:107241浏览

本文主要和大家分享 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!\r\n");<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[&#39;HTTP_HOST&#39;];//获取服务器地址<br/>    // $url=$url.$_SERVER[&#39;PHP_SELF&#39;];//获取当前服务器下的文件名和目录<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 = &#39;3.doc&#39;;<br/>$wordName =  explode(&#39;.&#39;,$fileName)[0];$wordExt = explode(&#39;.&#39;,$fileName)[1];//获取当前文件下的目录<br/>$file_Name=dirname(__FILE__);<br/>if (is_file($wordName.&#39;.html&#39;)) {  <br/>echo file_get_contents($wordName.&#39;.html&#39;);<br/>}else{<br/>  word2html("$file_Name\\".$wordName.".".$wordExt,"$file_Name\\".$wordName.".html");//要转换的word文件和转换成的html的文件名}<br/></span>

可以看到当前目录的1.docx会生成1.html文件,另外网站展示1.html

相关推荐:

php将字符串转为html实体的示例开发

如何将Excel文件转换为Html的详解(图)

如何将Excel文件转换为Html(图文)

以上是PHP实现转换word为HTML并在线预览的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn