2012-07-03 62 views
4

我想将页面导出到.doc文件,但是当我打开生成的.doc文件时,它会在weblayout-view而不是打印视图中打开。这是丑陋和混乱。有没有办法将其设置为打印视图?在PHP中生成的Word文档的显示模式

我用它来生成文档的脚本:

<?php if(isset($_GET['word'])) { 
    header("Content-Type: application/vnd.ms-word"); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("content-disposition: attachment;filename=test.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>Example</title> 
    <style type="text/css"> /* SOME STYLING */ </style> 
</head> 
<body> 
    <h1>Hello StackOverflow!</h1> 
    <p>Lorem ipsum...</p> 
</body> 
</html> 

而现在,我在这里是可以添加文字复选框☒和文字输入字段?

+1

你意识到你不是真正创建一个Word文档....这是HTML标记与头假装这是一个Word文档,其中MS Word中是慷慨地打开;这就是为什么它会在Web布局视图中打开它,因为它是一个Web文档。 –

+0

那么将html导出为文档的正确方法是什么? – Gijs

+1

正确的方法是生成一个实际的文档文件(BIFF或OfficeOpenXML) –

回答

5

这为我做的伎俩:

<?php 
header("Cache-Control: ");// leave blank to avoid IE errors 
header("Pragma: ");// leave blank to avoid IE errors 
header("Content-type: application/octet-stream"); 
header("content-disposition: attachment;filename=FILENAME.doc"); 
?> 
<html xmlns:v="urn:schemas-microsoft-com:vml" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:w="urn:schemas-microsoft-com:office:word" 
xmlns="http://www.w3.org/TR/REC-html40"> 

<head> 
<meta http-equiv=Content-Type content="text/html; charset=utf-8"> 
<meta name=ProgId content=Word.Document> 
<meta name=Generator content="Microsoft Word 9"> 
<meta name=Originator content="Microsoft Word 9"> 
<!--[if !mso]> 
<style> 
v\:* {behavior:url(#default#VML);} 
o\:* {behavior:url(#default#VML);} 
w\:* {behavior:url(#default#VML);} 
.shape {behavior:url(#default#VML);} 
</style> 
<![endif]--> 
<title>title</title> 
<!--[if gte mso 9]><xml> 
<w:WordDocument> 
    <w:View>Print</w:View> 
    <w:DoNotHyphenateCaps/> 
    <w:PunctuationKerning/> 
    <w:DrawingGridHorizontalSpacing>9.35 pt</w:DrawingGridHorizontalSpacing> 
    <w:DrawingGridVerticalSpacing>9.35 pt</w:DrawingGridVerticalSpacing> 
</w:WordDocument> 
</xml><![endif]--> 
<style> 
</head> 
<body> 
    Yes printview! 
</body> 
</html> 
+1

只有一个问题...“另存为”默认为“另存为网页”,而不是.doc(x) – Gijs