zoukankan      html  css  js  c++  java
  • 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);
            }
  • 相关阅读:
    第三章 AjaxPro框架
    第一章 ASP.NET XML与JSON
    第二章 ASP.NET Ajax核心对象
    第五次作业
    第四次作业
    第三周笔记
    第二周笔记
    Java作业
    日期顺时,自动跳过节假日
    利用java实现excel转pdf文件
  • 原文地址:https://www.cnblogs.com/zlp520/p/13495642.html
Copyright © 2011-2022 走看看