在PHP中读/写MS Word文件 [英] Reading/Writing a MS Word file in PHP

查看:112
本文介绍了在PHP中读/写MS Word文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不使用COM对象的情况下用PHP读取和写入Word(2003和2007)文件? 我知道我可以做到:

Is it possible to read and write Word (2003 and 2007) files in PHP without using a COM object? I know that I can:

$file = fopen('c:\file.doc', 'w+');
fwrite($file, $text);
fclose();

但是Word会将其读取为HTML文件而不是本机.doc文件.

but Word will read it as an HTML file not a native .doc file.

推荐答案

读取二进制Word文档将涉及根据DOC格式的已发布文件格式规范创建解析器.我认为这不是真正可行的解决方案.

Reading binary Word documents would involve creating a parser according to the published file format specifications for the DOC format. I think this is no real feasible solution.

您可以使用 Microsoft Office XML格式来读写Word文件-这与Word的2003和2007版本兼容.为了阅读,您必须确保Word文档以正确的格式保存(在Word 2007中称为Word 2003 XML文档).对于编写,您只需要遵循公开可用的XML模式即可.我从未使用过这种格式来从PHP编写Office文档,而是将其用于读取Excel工作表(自然保存为XML-Spreadsheet 2003)并将其数据显示在网页上.由于这些文件显然是XML数据,因此可以在其中导航并弄清楚如何提取所需的数据.

You could use the Microsoft Office XML formats for reading and writing Word files - this is compatible with the 2003 and 2007 version of Word. For reading you have to ensure that the Word documents are saved in the correct format (it's called Word 2003 XML-Document in Word 2007). For writing you just have to follow the openly available XML schema. I've never used this format for writing out Office documents from PHP, but I'm using it for reading in an Excel worksheet (naturally saved as XML-Spreadsheet 2003) and displaying its data on a web page. As the files are plainly XML data it's no problem to navigate within and figure out how to extract the data you need.

另一个选项-仅Word 2007选项(如果您的Word 2003中未安装OpenXML文件格式)-将重新排序为 databyss 指出的那样 MSDN 上有很多有关OpenXML文件格式的资源,因此您应该能够弄清楚如何读取所需的数据.我认为写作会更加复杂-取决于您要投入多少时间.

The other option - a Word 2007 only option (if the OpenXML file formats are not installed in your Word 2003) - would be to ressort to OpenXML. As databyss pointed out here the DOCX file format is just a ZIP archive with XML files included. There are a lot of resources on MSDN regarding the OpenXML file format, so you should be able to figure out how to read the data you want. Writing will be much more complicated I think - it just depends on how much time you'll invest.

也许您可以看看 PHPExcel ,该库能够编写到Excel 2007文件并使用OpenXML标准从Excel 2007文件读取.尝试读写OpenXML Word文档时,您可能会了解其中涉及的工作.

Perhaps you can have a look at PHPExcel which is a library able to write to Excel 2007 files and read from Excel 2007 files using the OpenXML standard. You could get an idea of the work involved when trying to read and write OpenXML Word documents.

这篇关于在PHP中读/写MS Word文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆