在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);
答案 0 :(得分:4)
我想出来了。查看解决方案以阅读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”添加了一堆带有问号的钻石......我删除了它并且它完美无缺。
此外,SaveAs必须拥有完整路径,至少在本地,我添加了它以使其工作。
再次感谢您的帮助。
答案 1 :(得分:3)
我对COM一无所知,但在MSDN上浏览Word API文档,看起来你最好的选择是使用Document.SaveAs
将wsFormatFilteredHTML
保存到临时文件,然后服务那个HTML给用户。一定要选择过滤的 HTML,否则你将获得最好的标签汤永远。
答案 2 :(得分:0)
我需要正确的XHTML,Office不会给你(我不明白这一点)。如果需要,您可以使用JTidy或TagSoup等工具来修复HTML。参看http://slideguitarist.blogspot.com/2011/03/exporting-word-documents-to-html.html