【php教程】PHP中html word 互转方法分享

WBOY
Freigeben: 2016-07-25 08:45:53
Original
1410 Leute haben es durchsucht
在php开发中,php如何实现html word互转的呢?有什么方法吗?下面就给大家介绍php将html转入word中的方法,这是经过测试的,这种方法有一点不好,html页面代码要写在php中,不过好歹能运行,看程序
class word{
function start(){
ob_start();
echo '
="urn:schemas-microsoft-comfficeffice"
xmlns:w="urn:schemas-microsoft-comffice:word"
xmlns="http://www.w3.org/TR/REC-html40">';
}
function save($path)
{
echo "";
$data = ob_get_contents();
}
function wirtefile($fn,$data){
$fp=fopen($fp,$data);
fwrite($fp,$data);
}
}
$html='
姓名 性别 年龄 爱好 备注
张三 32 足球
李四
43 篮球
';
//批量生成
//for($i=1;$i
$word = new word();
$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,最好还是用windows office软件,libreoffice不能完美转换,wps没有api。
先确认com模块是不是开启,phpinfo里面如果有com_dotnet模块,说明已开启,如果没有,修改php.ini,
代码如下:
com.allow_dcom = true
前面的注释去掉,重启就OK了,php官方网站说,php5.4.5之前,com模块是内置的,其实也不一定全是,官网下的php 5.3.39,com模块就没有内置。
如果不是内置模块的话,php.ini加上,前提你的ext文件夹下,有该扩展
代码如下:
extension=php_com_dotnet.dll
然后重启就OK了
function word2html($wordname,$htmlname)
{
$word = new COM("word.application") or die("Unable to instanciate Word");
$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. 如果页面一直在加载,把文档重命名,然后在重新转。
以上就是本文全部内容,有兴趣的朋友可以自己试试,若有更好的实现html word互转的方法,欢迎分享给大家。
推荐阅读:php 中,数据排序的 无限极分类的 常用的分析手法



Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!