将PHP文件转换为Word doc并在浏览器中打开它

时间:2015-05-30 16:32:30

标签: php phpword

我喜欢将我的PHP文件转换为Word文档,我使用了这段代码

<?php
    header("Content-type: application/vnd.ms-word");
    header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">
<title>Saves as a Word Doc</title>
</head>
<body>
<h1>Header</h1>
  This text can be seen in word
<ul>
<li>List 1</li>
<li>List 2</li>
</ul>
</body>
</html>

它工作正常,它提示我打开/保存文档,而不是像普通网页一样呈现它。但我也需要在浏览器中打开同一页面。

有没有办法在Web浏览器中打开PHP页面,并将其保存为word doc。如果可能的话,给我一些代码,通过单击按钮提示保存为Word文档。

1 个答案:

答案 0 :(得分:2)

希望下载参数时传递参数:

<?php
if( $_GET['action'] == 'download' ) {
  header("Content-type: application/vnd.ms-word");
  header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<? } ?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">
<title>Saves as a Word Doc</title>
</head>
<body>
<h1>Header</h1>
  This text can be seen in word
<ul>
<li>List 1</li>
<li>List 2</li>
</ul>
</body>
</html>

然后有一个通过file.php?action=download

的链接