【问题标题】:How do you display a formatted Word Doc in HTML/PHP?如何在 HTML/PHP 中显示格式化的 Word Doc?
【发布时间】:2011-03-17 02:40:45
【问题描述】:

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

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

$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);

【问题讨论】:

  • 我希望大声笑...我发布的代码有效,但无论如何它都没有格式化。
  • 您需要一些可以解析 Word 格式语法并将其转换为 HTML 和 CSS 的软件。
  • 服务器(或任何无人值守的环境)上的办公自动化并不安全,也不推荐使用,而且本质上是一次性的。不要在服务器上执行此操作。

标签: php ms-word openxml doc


【解决方案1】:

我想通了。查看阅读 Word Doc 并将其格式化为 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”会添加一堆带问号的菱形...我删除了它,它运行良好。

此外,SaveAs 必须具有完整路径,至少在本地,我添加了它以使其工作。

再次感谢您的帮助。

【讨论】:

    【解决方案2】:

    我对 COM 一无所知,但在 MSDN 上的 Word API 文档中查找,看起来你最好的选择是使用 Document.SaveAswsFormatFilteredHTML 保存到一个临时文件,然后将该 HTML 提供给用户。一定要选择 filtered HTML,否则你会得到最糟糕的标签汤 ever

    【讨论】:

    • 无法正常工作...您还有其他建议吗?
    • 不幸的是,这就是我所拥有的一切。
    【解决方案3】:

    我需要正确的 XHTML,Office 不会给你(我确实理解)。如果需要,您可以使用 JTidy 或 TagSoup 等工具来修复 HTML。参照。 http://slideguitarist.blogspot.com/2011/03/exporting-word-documents-to-html.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 2011-02-21
      • 2021-09-06
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多