如何从 php 中的 .doc 模板创建一个 word .doc 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2000882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Create a word .doc file from a .doc template in php
提问by Nidhin Baby
I need to create a word document from a word template, its something like, we need to replace the few strings (say variables) in the template with the values.
我需要从 word 模板创建一个 word 文档,就像我们需要用值替换模板中的几个字符串(比如变量)。
Please tell how we can do this in PHP ( or cakePHP).
请告诉我们如何在 PHP(或 cakePHP)中做到这一点。
回答by Colin O'Dell
I'd recommend checking out phpLiveDocx: http://www.phplivedocx.org/
我建议查看 phpLiveDocx:http://www.phplivedocx.org/
And some other questions on SO that might be worth reading:
以及其他一些可能值得阅读的关于 SO 的问题:
回答by Dereleased
The biggest problem you will run into is that the .docformat is binary in nature (see this questionand this post by Joel).
您将遇到的最大问题是.doc格式本质上是二进制的(请参阅此问题和Joel 的这篇文章)。
Is there any reason you cannot using either an HTML formatted document (which word has been able to open for over 10 years) or a Rich Text File? Both of these are text based, and will cause you significantly less pain. They are, as a result, very easy to use a template with.
是否有任何原因不能使用 HTML 格式的文档(哪个词可以打开超过 10 年)或富文本文件?这两个都是基于文本的,并且会显着减少您的痛苦。因此,它们非常易于使用模板。
Alternatively, if you can be sure that the user will have a newer version of Office (2007+) then you could, though it is somewhat tricky, use a .docxdocument, which is actually a Zip Archive and XML data. You can implement this fairly simply by creating a .docxword file that has the template you need, renaming it to .zip, extracting the files, teaching your application to do the text replacement on this in-place file structure, re-zip it using PHP's ZipArchive class, and then sending that file (named .docx, of course).
或者,如果您可以确定用户将使用较新版本的 Office (2007+),那么您可以使用一个.docx文档,尽管这有点棘手,但它实际上是一个 Zip 存档和 XML 数据。您可以通过创建一个.docx包含您需要的模板的word 文件,将其重命名为.zip,提取文件,教您的应用程序在这个就地文件结构上进行文本替换,使用PHP 的 ZipArchive 类重新压缩它来实现这一点,然后发送该文件(.docx当然名为)。
回答by Myhome Stories
i think this will help you
我想这会帮助你
docxtemplater-master
docxtemplater-master
Generate docx from templates, https://github.com/edi9999/docxtemplater
从模板生成 docx, https://github.com/edi9999/docxtemplater
phpdocx
phpdocx
PHP library for the dynamical generation of Word documents in docx format.
用于动态生成 docx 格式的 Word 文档的 PHP 库。
回答by Krab
Check http://www.phpbuilder.com/annotate/message.php3?id=1024286and also the article it comments.
检查http://www.phpbuilder.com/annotate/message.php3?id=1024286以及它评论的文章。
I made a similar trick when I needed to generate a simple excel table. When sending the right header, the client's excel didn't complain at all. Or if you can use another format directly, use it. (.docx or open document are compressed xml, so you can manipulate it more easily)
当我需要生成一个简单的 excel 表格时,我做了一个类似的技巧。发送正确的标头时,客户的 excel 根本没有抱怨。或者如果您可以直接使用其他格式,请使用它。(.docx 或打开的文档是压缩的 xml,因此您可以更轻松地对其进行操作)

