如何在 HTML/PHP 中显示格式化的 Word 文档?

发布于 2024-10-22 22:36:16 字数 420 浏览 6 评论 0原文

在 HTML/PHP 中显示格式化的 Word 文档的最佳方式是什么?

这是我目前拥有的代码,但它没有格式化它:

$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("ACME.doc"));

// Extract content.
$content = (string) $word->ActiveDocument->Content;

echo $content;

$word->ActiveDocument->Close(false);

$word->Quit();
$word = null;
unset($word);

What is the best way to display a formatted Word Doc in HTML/PHP?

Here is the code I currently have but it doesn't format it:

$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("ACME.doc"));

// Extract content.
$content = (string) $word->ActiveDocument->Content;

echo $content;

$word->ActiveDocument->Close(false);

$word->Quit();
$word = null;
unset($word);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯下孤影 2024-10-29 22:36:16

我明白了这一点。查看读取 Word 文档并将其格式化为 HTML 的解决方案:

$filename = "ACME.doc";
$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath($filename));

$new_filename = substr($filename,0,-4) . ".html";

// the '2' parameter specifies saving in txt format
// the '6' parameter specifies saving in rtf format
// the '8' parameter specifies saving in html format
$word->Documents[1]->SaveAs("C:/a1/projects/---full path--- /".$new_filename,8);
$word->Documents[1]->Close(false);
$word->Quit();
//$word->Release();
$word = NULL;
unset($word);

$fh = fopen($new_filename, 'r');
$contents = fread($fh, filesize($new_filename));
echo $contents;
fclose($fh);
//unlink($new_filename);

有几件事...在我的 PHP 页面顶部添加了“charset=UTF-8”,添加了一堆带有问号的菱形...我删除了那个并且它工作得很好。

另外,另存为必须具有完整路径,至少在本地,我添加了它才能使其正常工作。

再次感谢您的帮助。

I figured this out. Check out the solution to reading a Word Doc and formatting it in HTML:

$filename = "ACME.doc";
$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath($filename));

$new_filename = substr($filename,0,-4) . ".html";

// the '2' parameter specifies saving in txt format
// the '6' parameter specifies saving in rtf format
// the '8' parameter specifies saving in html format
$word->Documents[1]->SaveAs("C:/a1/projects/---full path--- /".$new_filename,8);
$word->Documents[1]->Close(false);
$word->Quit();
//$word->Release();
$word = NULL;
unset($word);

$fh = fopen($new_filename, 'r');
$contents = fread($fh, filesize($new_filename));
echo $contents;
fclose($fh);
//unlink($new_filename);

Couple of things... Having "charset=UTF-8" at the top of my PHP page was adding a bunch of diamonds with questions marks... I deleted that and it works perfectly.

Also, the SaveAs has to have the full path, at least locally, I added that to get it to work.

Thanks again for your help.

嘴硬脾气大 2024-10-29 22:36:16

我对 COM 一无所知,但是浏览 MSDN 上的 Word API 文档,看起来您最好的选择是使用 Document.SaveAs 另存为 wsFormatFilteredHTML 到临时文件,然后将该 HTML 提供给用户。请务必选择过滤 HTML,否则您将得到有史以来最糟糕的标签汤。

I know nothing about COM, but poking around the Word API docs on MSDN, it looks like your best bet is going to be using Document.SaveAs to save as wsFormatFilteredHTML to a temporary file, then serving that HTML to the user. Be sure to pick the filtered HTML, otherwise you're going to get the soupiest tag soup ever.

纵情客 2024-10-29 22:36:16

我需要正确的 XHTML,但 Office 不会提供该信息(我理解这一点)。如果需要,您可以使用 JTidy 或 TagSoup 等工具来修复 HTML。比照。 http://slideguitarist.blogspot.com/2011/03 /导出-word-documents-to-html.html

I needed correct XHTML, which Office won't give you (I do not understand that). You can use tools such as JTidy or TagSoup to fix the HTML, if you need to. Cf. http://slideguitarist.blogspot.com/2011/03/exporting-word-documents-to-html.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文