gpt4 book ai didi

c# - 以编程方式比较word文档

转载 作者:太空狗 更新时间:2023-10-29 21:41:09 26 4
gpt4 key购买 nike

我需要比较两个office文档,在本例中是两个word文档并提供差异,这有点类似于SVN中显示的内容。不至于到那种程度,但至少能够突出差异。

我尝试使用 office COM dll 并得到了这一步..

object fileToOpen = (object)@"D:\doc1.docx";
string fileToCompare = @"D:\doc2.docx";

WRD.Application WA = new WRD.Application();

Document wordDoc = null;

wordDoc = WA.Documents.Open(ref fileToOpen, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wordDoc.Compare(fileToCompare, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

关于如何进一步进行的任何提示?这将是一个具有大量点击率的 Web 应用程序。使用 office com 对象是正确的方法吗,或者还有其他我可以看的东西吗?

最佳答案

您应该使用 Document 类来比较文件并在 Word 文档中打开结果。

using OfficeWord = Microsoft.Office.Interop.Word;

object fileToOpen = (object)@"D:\doc1.docx";
string fileToCompare = @"D:\doc2.docx";

var app = Global.OfficeFile.WordApp;

object readOnly = false;
object AddToRecent = false;
object Visible = false;

OfficeWord.Document docZero = app.Documents.Open(fileToOpen, ref missing, ref readOnly, ref AddToRecent, Visible: ref Visible);

docZero.Final = false;
docZero.TrackRevisions = true;
docZero.ShowRevisions = true;
docZero.PrintRevisions = true;

//the OfficeWord.WdCompareTargetNew defines a new file, you can change this valid value to change how word will open the document
docZero.Compare(fileToCompare, missing, OfficeWord.WdCompareTarget.wdCompareTargetNew, true, false, false, false, false);

关于c# - 以编程方式比较word文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8244917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com