一、PHP生成word的两种思路或原理
1.利用windows下面的 com组件
2.利用PHP将内容写入doc文件之中
具体实现方法如下。
二、利用windows下面的com组件
原理:com作为PHP的一个扩展类,安装过office的服务器会自动调用word.application的com,可以自动生成文档,PHP官方文档手册:http://:office:word"
xmlns="http://www.w3.org/TR/REC-html40">';
}
function save($path)
{
echo "</html>";
$data = ob_get_contents();
ob_end_clean();
$this->wirtefile ($path,$data);
}
function wirtefile ($fn,$data)
{
$fp=fopen($fn,"wb");
fwrite($fp,$data);
fclose($fp);
}
}
复制代码 代码如下:$html = '
<table width=600 cellpadding="6" cellspacing="1" bgcolor="#336699">
<tr bgcolor="White">
<td>PHP10086</td>
<td><a href="https://www.jb51.net" target="_blank" >https://www.jb51.net</a></td>
</tr>
<tr bgcolor="red">
<td>PHP10086</td>
<td><a href="https://www.jb51.net" target="_blank" >https://www.jb51.net</a></td>
</tr>
<tr bgcolor="White">
<td colspan=2 >
PHP10086<br>
最靠谱的PHP技术分享网站
<img src="https://www.jb51.net/wp-content/themes/WPortal-Blue/images/logo.gif">
</td>
</tr>
</table>
';
//批量生成
for($i=1;$i<=3;$i++){
$word = new word();
$word->start();
//$html = "aaa".$i;
$wordname = 'PHP教程网站--jb51.net'.$i.".doc";
echo $html;
$word->save($wordname);
ob_flush();//每次执行前刷新缓存
flush();
}
个人点评:这种方法效果最好,原因有三个:
第一代码比较简洁,很容易理解
第二是支持批量生成word(这个很重要)
第三是支持完整的html代码