2011-03-17 135 views
3

什麼是顯示一個格式化的Word文檔中的HTML/PHP的最佳方法?如何在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); 
+0

這可能嗎? – alex 2011-03-17 02:45:03

+0

我希望大聲笑...我發佈的代碼的作品,但它仍然沒有格式它的反正。 – 2011-03-17 02:47:31

+1

您需要一些軟件來解析Word格式的文法並將其轉換爲HTML和CSS。 – alex 2011-03-17 02:49:59

回答

3

我一無所知COM,而是圍繞MSDN上的字API文檔戳,它看起來像你最好的選擇是怎麼回事要使用Document.SaveAswsFormatFilteredHTML保存到臨時文件,然後服務於HTML給用戶。一定要挑過濾 HTML,否則你會得到soupiest標籤湯不斷

+0

謝謝,我現在正在嘗試使用它。 – 2011-03-17 03:00:47

+0

無法獲得此工作...您有任何其他建議嗎? – 2011-03-17 03:38:48

+0

這就是我所擁有的,不幸的是。 – Charles 2011-03-17 04:05:42

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

夫婦的事情......有了「字符集= UTF-8」在我的PHP頁面的頂部被添加了一堆鑽石問題標記...我刪除了它,它完美的作品。

此外,SaveAs必須具有完整的路徑,至少在本地,我補充說,讓它工作。

再次感謝您的幫助。

相關問題