您的位置:首页 > 编程语言 > PHP开发

PHP生成word文档

2018-01-06 15:47 786 查看
ob_start(); //打开缓冲区
echo '
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<xml><w:WordDocument><w:View>Print</w:View></xml>
</head>';
echo '<body>
<h1 style="text-align: center">这是一个word文档</h1>
<img src="D:\phpStudy\WWW\111.png" alt="" />
</body>';
header("Cache-Control: no-store"); //所有缓存机制在整个请求/响应链中必须服从的指令
Header("Content-type: application/octet-stream");  //用于定义网络文件的类型和网页的编码,决定文件接收方将以什么形式、什么编码读取这个文件
Header("Accept-Ranges: bytes");  //Range防止断网重新请求 。
if (strpos($_SERVER["HTTP_USER_AGENT"],'MSIE')) {
header('Content-Disposition: attachment; filename=test.doc');
}else if (strpos($_SERVER["HTTP_USER_AGENT"],'Firefox')) {
Header('Content-Disposition: attachment; filename=test.doc');
} else {
header('Content-Disposition: attachment; filename=test.doc');
}
header("Pragma:no-cache"); //不能被浏览器缓存
header("Expires:0");  //页面从浏览器高速缓存到期的时间分钟数,设定expires属性为0,将使对一页面的新的请求从服务器产生
ob_end_flush();//输出全部内容到浏览器
注意:
1.所有样式必须写在行内,不然word不识别。
2.引入图片必须要写绝对路径。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php word