使用 Office Word 通过 PHP 读取 doc 文件
我正在尝试使用 PHP 和 word.application
来读取文件。它根本不会打开该文件。它呼应了正确的版本。
$w = new COM("word.application") or die("Is office installed?");
echo 'Loaded Word, version ' . $w->Version . '<br>';
$w->Visible = false;
$w->Documents->Open(realpath('test.docx'));
$content = (string) $w->ActiveDocument->Content;
echo $content;
$w->Quit();
$w->Release();
$w = null;
我收到错误:
Uncaught exception 'com_exception' with message 'Source: Microsoft Word
Description: This command is not available because no document is open.'
感觉像是某种权限问题。除了使用 realpath 之外,我还尝试放置 test.docx 的路径,但这没有帮助。还尝试把它放到我的C盘根目录下。我使用的是 Windows 7 Professional 和 Microsoft Office 2007。
I am trying to use PHP with word.application
to read a file. It simply will not open the file. It's echoing the right version.
$w = new COM("word.application") or die("Is office installed?");
echo 'Loaded Word, version ' . $w->Version . '<br>';
$w->Visible = false;
$w->Documents->Open(realpath('test.docx'));
$content = (string) $w->ActiveDocument->Content;
echo $content;
$w->Quit();
$w->Release();
$w = null;
I get the error:
Uncaught exception 'com_exception' with message 'Source: Microsoft Word
Description: This command is not available because no document is open.'
It feels like it's some kind of permission problem. I tried to put the path of the test.docx
besides using realpath and that did not help. Also tried to put it in the root of my C drive. I am using Windows 7 Professional and Microsoft Office 2007.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果一切正常,文档->打开将返回一个文档。可能该文档不存在(路径不正确),或者您没有从 PHP 打开它的权限。将结果存储在 $var 中,检查它是否具有适当的值(如果不是,则可能是
not isset
、null
或false
),然后使用 $ var->Content 读取内容。Documents->Open returns a document if all is ok. Probably, the document does not exists (path incorrect), or you haven't got the rights to open it from PHP. Store the result in $var, check if it has an appropriate value (probably
not isset
,null
orfalse
if not), and use $var->Content to read the content.文件/路径。
file_get_contents 看看是否可以
读它。
如果一切正常 - 那么这不是权限/等问题。
file/path.
file_get_contents and see if you can
read it.
If that all works - then it's not a problem with permissions/etc.