Home  >  Article  >  Backend Development  >  PHP converts word to HTML and previews it online

PHP converts word to HTML and previews it online

小云云
小云云Original
2018-05-28 16:46:107294browse

本文主要和大家分享 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(图文)

The above is the detailed content of PHP converts word to HTML and previews it online. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn