比较 C# Web 应用程序中 asp.net 两个 Word 文档



我要求比较两个word文档并显示差异。 我正在为此而努力。

我通过搜索得到了一些代码:

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    wordApp.Visible = false;
    wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
    object wordTrue = (object)true;
    object wordFalse = (object)false;
    object fileToOpen = @"D:doc test filesdoc1.docx";
    object missing = Type.Missing;
    Microsoft.Office.Interop.Word.Document doc1 = wordApp.Documents.Open(ref fileToOpen,
       ref missing, ref wordFalse, ref wordFalse, ref missing,
       ref missing, ref missing, ref missing, ref missing,
       ref missing, ref missing, ref wordTrue, ref missing,
       ref missing, ref missing, ref missing);
    object fileToOpen1 = @"D:doc test filesdoc2.docx";
    Microsoft.Office.Interop.Word.Document doc2 = wordApp.Documents.Open(ref fileToOpen1,
        ref missing, ref wordFalse, ref wordFalse, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing);
    Microsoft.Office.Interop.Word.Document doc = wordApp.CompareDocuments(doc1, doc2, WdCompareDestination.wdCompareDestinationNew, WdGranularity.wdGranularityWordLevel,
        true, true, true, true, true, true, true, true, true, true, "", false);
    doc1.Close(ref missing,ref missing,ref missing);
    doc2.Close(ref missing, ref missing, ref missing);

因此,当我运行此代码时,打开doc1和doc2
时会出现一条消息

Doc1.docx 被锁定,由 xxxxx 编辑

和可用选项:

  1. 打开只读副本
  2. 创建本地副本并稍后合并更改
  3. 当原始副本可用时接收通知。

我需要避免这些消息。 有人可以对此有想法吗?

您可以使用选项 2 并创建文档的副本。由于您只想知道差异并且不打算修改文档本身,因此这应该可以工作。

但请注意 Web 应用程序中多线程可能存在的问题。不要只是将文件复制到临时目录。在用户 1 想要检查 test 的情况下.docx并且用户 2 也有测试.docx他们将覆盖 test.docx而您再次遇到同样的问题。若要启用此功能,可以使用唯一名称(如 GUID)重命名本地副本。

最新更新