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

2025-10-27
网站建设限时活动促销

本文主要和大家分享 php使用com组件转换word文档为html并实现在线预览,希望能帮助到大家。

PHP5.4

[CoM];
path to a file containing GUlDs,llDs or filenames of files with TypeLibs;
http://php.net/com.typelib-file;com.typelib_file=

;allow Distributed-COM calls
;http://php.cn/com.allow-dcomcom.allow dcom= true

PHP7.0

在php.ini中增加

[COM_DOT_NET]extension=php_com_dotnet.dll

重启 php即可.

在根目录输入

立即学习“PHP免费学习笔记(深入)”;

error_reporting(E_ALL);
ini_set("display_errors","On");
$excel = new COM("Excel.application") or die ("ERROR: Unable to instantaniate COM!\r\n");

print_r(get_loaded_extensions());?>

可以数组形式罗列php的现有组件.

执行程序

新建index.php, 默认传入3.doc , 然后运行就可以查看效果了.

 //获取链接地址

//去除目录中的文件名
$word = new COM("word.application") or die("找不到 Word 程序"); // 建立一个指向新COM组件的索引
// 显示目前正在使用的Word的版本号
//echo "Loading Word, v. {$word->Version}
";
// 把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真)
$word->Visible = 0;
$word->Documents->Open($wordname) or die("无法打开这文件");

header("Content-Type: text/html;charset=gb2312");//设置文件的格式
//打开一个文档
//把文档保存在目录中
try{
$word->Documents[1]->SaveAs($htmlname,8);
} catch(Exception $e){
print $e->getMessage();
}
$content=file_get_contents($htmlname);
echo $content;//输出word文档的内容
// 关闭与COM组件之间的连接
$word->Quit();
unset($word);
}
$fileName = '3.doc';
$wordName = explode('.',$fileName)[0];$wordExt = explode('.',$fileName)[1];//获取当前文件下的目录
$file_Name=dirname(__FILE__);
if (is_file($wordName.'.html')) {
echo file_get_contents($wordName.'.html');
}else{
word2html("$file_Name\\".$wordName.".".$wordExt,"$file_Name\\".$wordName.".html");//要转换的word文件和转换成的html的文件名}

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

相关推荐:

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

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

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

标签: 文件格式转换器在线转换

本文地址:https://www.lifejia.cn/news/208110.html

免责声明:本站内容仅用于学习参考,信息和图片素材来源于互联网,如内容侵权与违规,请联系我们进行删除,我们将在三个工作日内处理。联系邮箱:cloudinto#qq.com(把#换成@)