天天看點

把Word文檔轉化為Html檔案

  public static bool WordToHtml(string wordFileName, string htmlFileName)       

    {           

        Object oMissing = System.Reflection.Missing.Value;           

        Microsoft.Office.Interop.Word._Application WordApp = new Microsoft.Office.Interop.Word.Application();           

        //使UI不現實           

        WordApp.Visible = false;           

        object filename = wordFileName;           

        //打開檔案           

        Microsoft.Office.Interop.Word.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 docType = WordDoc.GetType();           

        object saveFileName = htmlFileName;           

        docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc,                new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML });           

        //儲存           

        WordDoc.Save();           

        //WordDoc.SaveAs2(saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML);           

        WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);           

        WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);           

        return true;       

    }