標籤:winform style blog color io os for 檔案 sp
public static bool WordToHtml(string wordFileName, string htmlFileName) { try { Object oMissing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word._Application WordApp = new Microsoft.Office.Interop.Word.Application(); WordApp.Visible = false; object filename = wordFileName; _Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); // Type wordType = WordApp.GetType(); // 開啟檔案 Type docsType = WordApp.Documents.GetType(); // 轉換格式,另存新檔 Type docType = WordDoc.GetType(); object saveFileName = htmlFileName; docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc, new object[] { saveFileName, WdSaveFormat.wdFormatHTML }); //儲存 WordDoc.Save(); WordDoc.Close(ref oMissing, ref oMissing, ref oMissing); WordApp.Quit(ref oMissing, ref oMissing, ref oMissing); return true; } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); return false; } }
上面的方法是在winform中解決有Word轉換成HTML網頁的,在轉換過程中,會產生一個網頁和一個檔案夾,如果想將這個網頁在前台顯示出來,第一步,需要將其這個網頁源碼中的XXX.files(轉換過程中產生的檔案夾的名稱),轉換成正確的路徑,但是這樣顯示出來就會有一個問題,那就是在別的瀏覽器好使 ,但是在IE瀏覽器中是什麼都不顯示的,也就是傳說中的不相容,那麼,接下來看我們的第二步.
第二步:刪除源檔案中的<![if !vml]> <![endif]><![if !vml]><![endif]>這四個標籤,或者用string.Replace()替換成空值,這樣就顯示出來了,業解決掉了不相容的問題
word轉換成HTML 以及IE不相容問題