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);
            }
  • 相关阅读:
    [OpenJudge90][序列DP+乱搞]滑雪
    [OpenJudge8786][暴力DP]方格取数
    [OpenJudge8782][划分DP]乘积最大
    [OpenJudge8471][划分DP]切割回文
    [OpenJudge8462][序列DP]大盗阿福
    【棋盘DP】【OpenJudge7614】最低通行费
    【OpenJudge8464】【序列DP】股票买卖
    bzoj1674: [Usaco2005]Part Acquisition 裸dijkstra
    bzoj3040 最短路+配对堆优化
    poj1330|bzoj3732|noip2013 货车运输 kruskal+倍增lca
  • 原文地址:https://www.cnblogs.com/zlp520/p/13495642.html
Copyright © 2011-2022 走看看