簡體   English   中英

比較ASP.NET C#Web應用程序中的兩個Word文檔

[英]Compare two word documents in asp.net C# web application

我需要比較兩個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 files\doc1.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 files\doc2.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應用程序中的多線程可能存在的問題。 不要只是將文件復制到臨時目錄。 在user1要檢查test.docx的情況下,user2也有一個test.docx,它們將覆蓋test.docx,並且您再次遇到相同的問題。 為此,您可以使用唯一名稱(如GUID)重命名本地副本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM