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}File\html\{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}File\html\{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);
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM