1

我在 php 中有这个简单的代码:

<?php

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=kid_tag.doc");

echo '<table cellspacing="0" cellpadding="0" border="0" width="8.4cm">
    <tr>
    <td colspan="3" style="text-align: right; height: 0.6cm">Nursery</td>
    </tr>
    <tr>
        <td style="height: 1.8cm"><img src="http://images.funadvice.com/photo/image/old/6943/tiny/cat.jpg" /></td>
        <td style="text-align: center; font-weight: bold">Sofia Abello</td>
    <td>&nbsp;</td>
    </tr> 
    <tr>
    <td style="text-align: left; height: 0.6cm">9AM Oct-12-08</td>
    <td>&nbsp;</td>
    <td style="text-align: right">Dance Studio</td>
    </tr>  
</table>';

?>

使用 MS Office Word 显示正常,但是使用打开的 Office 编写器打开时,宽度会减小(宽度不合适!)。

4

2 回答 2

1

我认为使用 PHP 生成 DOC 文件的最简单方法是使用 Zend Framework 组件phpLiveDocx。您可以加载 Word 或 Open Office 模板、合并文本数据并将最终文档保存为多种格式,例如 DOC、DOCX、RTF 和 PDF。

在项目网站上了解更多信息:

http://www.phplivedocx.org/articles/brief-introduction-to-phplivedocx/

于 2009-05-14T06:52:38.580 回答
0

您实际上是将 HTML 导入 MS Word 和 OpenOffice.org。HTML 不是 Word 和 OpenOffice.org 的原生格式,这意味着必须先转换输入。

毫不奇怪,这些应用程序(其主要目的是以应用程序的本机格式编辑文档)在那里做得并不完美。事实上——这并不是什么大秘密——即使是主要目的呈现 HTML 的网络浏览器在该领域也不完美。

解决方案是提供适用于两种应用程序的 HTML。您可以使用条件注释来做到这一点,条件注释是 Microsoft 对 HTML 的专有扩展,因此只有 Microsoft 产品才能理解。

这就是您的示例中的样子:

<![if !mso]>
<table cellspacing="0" cellpadding="0" border="0" width="8.4cm">
  <tr>
    <td>OpenOffice.org Version</td>
  </tr>
</table>
<![endif]>
<!--[if mso]>
<table cellspacing="0" cellpadding="0" border="0" width="8.4cm">
  <tr>
    <td>Microsoft Word version</td>
  </tr>
</table>
<![endif]-->
于 2009-04-09T21:12:48.660 回答