zoukankan      html  css  js  c++  java
  • C# 之 比较两个word文档的内容

      利用 Microsoft.Office.Interop.Word 组件进行比较。引入命名空间:using Word2013 = Microsoft.Office.Interop.Word;

      代码如下:

         /// <summary>
            /// 进行与Word2013.Document类创建时指定的文件比较,
            /// 然后将差异显示在targetFile,并保存退出
            /// </summary>
            /// <param name="sourceFile">源文件(修改前文件)</param>
            /// <param name="targetFile">目标文件(修改后文件)</param>
            public static void CompareWordFile(string sourceFile, string targetFile)
            {
                object missing = System.Reflection.Missing.Value;
                object sFileName = sourceFile;
                var tFileName = targetFile;
                var wordApp = new Word2013.Application();
                wordApp.Caption = "CompareWordApp";
                wordApp.Visible = false;
    
                var wordDoc = wordApp.Documents.Open(ref sFileName, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing);
    
                wordDoc.TrackRevisions = true;
                wordDoc.ShowRevisions = true;
                wordDoc.PrintRevisions = true;
    
                object comparetarget = Word2013.WdCompareTarget.wdCompareTargetSelected;
                object addToRecentFiles = false;
                wordDoc.Compare(tFileName, ref missing, ref comparetarget, ref missing, ref missing, ref addToRecentFiles,
                    ref missing, ref missing);
    
                int changeCount = wordApp.ActiveDocument.Revisions.Count;
                Object saveChanges = Word2013.WdSaveOptions.wdSaveChanges;
                wordDoc.Close(ref saveChanges, ref missing, ref missing);
                wordApp.Quit(ref saveChanges, ref missing, ref missing);
    
                KillWordProcess("CompareWordApp");
            }
  • 相关阅读:
    保持简单----纪念丹尼斯•里奇(Dennis Ritchie)
    转:有关retina和HiDPI那点事
    Powershell 学习
    Windows与Linux共享文件夹互相访问
    你知道C语言为什么会有“_”(下划线)吗?
    百度公共DNS
    AXIS2的一些认识
    待整理
    java复习汇总之面试篇
    落网歌曲图片下载
  • 原文地址:https://www.cnblogs.com/xinaixia/p/5749166.html
Copyright © 2011-2022 走看看