2009-05-25 152 views

回答

3

我覺得這是應該做的最簡單的方法是

http://asptutorials.net/C-SHARP/convert-ms-word-docs-to-html/

他們在文章中關鍵的一點是,他們所使用的另存爲功能 http://msdn.microsoft.com/en-us/library/aa220734.aspx

像這樣:

string newfilename = folder_to_save_in + FileUpload1.FileName.Replace(".doc", ".html"); 
    object o_nullobject = System.Reflection.Missing.Value;  
    object o_newfilename = newfilename; 
    object o_format = Word.WdSaveFormat.wdFormatHTML; 
    object o_encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; 
    object o_endings = Word.WdLineEndingType.wdCRLF; 
    // SaveAs requires lots of parameters, but we can leave most of them empty: 
    wordApplication.ActiveDocument.SaveAs(ref o_newfilename, ref o_format, ref o_nullobject, 
    ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, 
    ref o_nullobject, ref o_nullobject, ref o_encoding, ref o_nullobject, 
    ref o_nullobject, ref o_endings, ref o_nullobject); 

該庫是 Microsoft.Office.Interop.Word;

如果我沒記錯的話在執行代碼的機器上需要Word。如果它是ASP.NET,則在服務器上是必需的。

+0

我想你忘了在樣本中分配/聲明o_nullobject。它應該設置爲`System.Type.Missing` – 2009-05-25 12:44:26

+0

謝謝divo,忘記了是的。 – 2009-05-25 12:45:48

0

三種方式: 1.保存爲HTML,如napster 所述2.將Open XML轉換爲HTML; XSLT可在http://www.codeplex.com/OpenXMLViewer 3.對於最乾淨的HTML,編寫代碼將文檔中的每種樣式轉換爲CSS,並將@style中的任何直接格式化。

是否在運行C#代碼的計算機上安裝了Word?