php 用PHP生成word文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4153591/
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
Generating word documents with PHP
提问by EL_W
Do you know any way to generate doc
and docx
files with PHP and without COM component? I've tried PHPWord
, which creates docx files, but these cannot be opened in OpenOffice
because they cause it to crash. I've also tried PHPDocx
, but it didn't generate any files at all.
你知道的任何方法来生成doc
和docx
文件用PHP和不COM组件?我试过PHPWord
,它会创建 docx 文件,但无法打开这些文件,OpenOffice
因为它们会导致它崩溃。我也试过PHPDocx
,但它根本没有生成任何文件。
回答by SW4
See here:
看这里:
http://www.webcheatsheet.com/php/create_word_excel_csv_files_with_php.php
http://www.webcheatsheet.com/php/create_word_excel_csv_files_with_php.php
To quote from the article the most common method:
引用文章中最常用的方法:
Using HTTP Headers
使用 HTTP 标头
In this method you need to format the HTML/PHP page using Word-friendly CSS and add header information to your PHP script. Make sure you don't use external style sheets since everything should be in the same file.
As a result user will be prompted to download a file. This file will not be 100% "original" Word document, but it certainly will open in MS Word application. You can use this method both for Unix and Windows environments.
在这种方法中,您需要使用 Word 友好的 CSS 来格式化 HTML/PHP 页面,并将标题信息添加到您的 PHP 脚本中。确保您不使用外部样式表,因为所有内容都应该在同一个文件中。
结果将提示用户下载文件。该文件不会是 100% 的“原始”Word 文档,但它肯定会在 MS Word 应用程序中打开。您可以在 Unix 和 Windows 环境中使用此方法。
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");
echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "<b>My first document</b>";
echo "</body>";
echo "</html>";
?>
回答by edi9999
Generating word documents with JS:
用JS生成word文档:
I have created a simple open-source library that will replace tags by values.
我创建了一个简单的开源库,它将按值替换标签。
For example Hi {name}
with data={name:"John"}
will be replaced by Hi John
.
例如Hi {name}
withdata={name:"John"}
将被替换为Hi John
。
Here it is : https://github.com/edi9999/docxtemplater
这是:https: //github.com/edi9999/docxtemplater
回答by Skrol29
回答by Yavor
As I mentioned here PHP Convert Word file to HTML without losing styling and images,
正如我在这里提到的,PHP 将 Word 文件转换为 HTML 且不会丢失样式和图像,
The best solution I've found so far is http://www.phplivedocx.org/. You use it with the Zend framework. Very easy to set up and you get minimal deviation from the actual word format. It generates and converts word docs from/to html (among other formats) very well.
到目前为止,我找到的最佳解决方案是http://www.phplivedocx.org/。您可以将它与 Zend 框架一起使用。非常容易设置,您与实际单词格式的偏差最小。它可以很好地从/到 html(以及其他格式)生成和转换 word 文档。