2009-06-09 63 views
2

我正在尋找將html轉換爲MSWord的示例代碼。 C#代碼讚賞。 Html輸入是一個字符串,其內容是html文檔,我想學習如何使用.Net Word(Office)SDK來完成轉換。將html轉換爲MSWord

由於事先 喬治

+0

這是[類似的問題](http://stackoverflow.com/questions/32151/best-way-to-export-html-to-word-without-having-ms-word-installed)George but the鏈接與我之前的回答相同t關於HTML編輯器的問題;-) – Shoban 2009-06-09 18:13:48

回答

4

這取決於你正試圖轉換的HTML文件的性質了很多。一種簡單的方法是使用Word自動化來打開.html文檔,然後將其保存爲.doc文檔。

 object readOnly = false; 
     object isVisible = true; 
     object missing = System.Reflection.Missing.Value; // Values we don't care about 
     object fileName = "C:/webpage.htm"; 
     object newFileName = "C:/webpage.doc";  

     Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 

     // word.Visible = true; // To see what's happening 

     Microsoft.Office.Interop.Word.Document document = word.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 

     document.Activate(); 

     object saveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument; 

     document.SaveAs(ref newFileName, ref saveFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 

     document.Close(ref missing, ref missing, ref missing); 

  • 你必須增加的Microsoft.Office.Interop.Word的引用或類似的東西
  • 裁判失蹤參數的數量取決於Word中的您至極版本使用
  • 由於Word實例從System文件夾開始,因此必須在文件名中使用完整路徑。
0

請記住MSIE(或Word's,AFAIK MS Office仍然使用它自己的)渲染引擎並不像您希望的那樣可靠,所以除簡單格式之外的任何內容都可能看起來不同在Word文檔中而不是在瀏覽器中。

Alsoplustoo,Word對DOC格式的解釋可能與您的轉換器有所不同 - OO.o有這個問題的年齡。