c# word转html(另存为)

c# word转html(另存为)

使用注意:

1.引用com类库:安装office后会有此类库,注意版本14.0是office2010版本:

 

方案一:

 /// <summary>
        /// word另存为html
        /// </summary>
        /// <param name="filePath"></param>
        private void WordToHTML(string filePath)
        {
            var rootPath = AppDomain.CurrentDomain.BaseDirectory;
            var fileName = Path.GetFileNameWithoutExtension(filePath);
            object nothing = Missing.Value;
            object newPath = string.Format(@"{0}Filehtml{1}.html", rootPath, fileName);
            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML;
            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Document document = application.Documents.Add(filePath, nothing, nothing);
            document.SaveAs2(newPath, format, nothing, nothing, nothing, nothing, nothing, nothing,
                nothing, nothing, nothing, Encoding.UTF8.CodePage, nothing, nothing, nothing);
            document.Close(nothing, nothing, nothing);
            application.Quit(nothing, nothing, nothing);
        }

方案二:

 /// <summary>
        /// word另存为html
        /// </summary>
        /// <param name="filePath"></param>
        private void WordToHTML2(string filePath)
        {
            var rootPath = AppDomain.CurrentDomain.BaseDirectory;
            var fileName = Path.GetFileNameWithoutExtension(filePath);
            object newPath = string.Format(@"{0}Filehtml{1}.html", rootPath, fileName);
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = word.GetType();
            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
            Type docsType = docs.GetType();
            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
            System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { filePath, true, true });
            Type docType = doc.GetType();
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
            null, doc, new object[] { newPath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }
原文地址:https://www.cnblogs.com/zlp520/p/13495642.html