我需要比较两个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
锁定以进行编辑
和可用选项:
我需要避免这些消息。任何人都可以对此有所了解吗?
答案 0 :(得分:0)
您可以使用选项2并创建文档的副本。由于您只想知道差异而不打算修改文档本身,这应该可行。
但请注意Web应用程序中多线程可能存在的问题。不要只是将文件复制到临时目录。在user1想要检查test.docx并且user2也有test.docx的情况下,它们将覆盖test.docx并且你再次遇到同样的问题。为此,您可以使用GUID等唯一名称重命名本地副本。