文件比較 C# 教程 – 程式化比較多個 Word 文件
有沒有發現自己手動逐行比較 Word 文件,想捕捉每一處變更?你並不孤單。在本指南中,你將學會如何高效比較多個 Word 文件,無論是審核法律合約、追蹤修訂,或是管理協同編輯專案。使用 GroupDocs.Comparison for .NET 自動化此過程,可節省時間、減少錯誤,並僅用幾行 C# 程式碼產生專業的比較報告。
本教程你將掌握的內容:
- 如何使用串流比較 Word 文件(適用於資料庫儲存的檔案)
- 從頭開始在 C# 專案中設定 GroupDocs.Comparison
- 使用專業樣式自訂比較結果
- 高效處理多文件比較
- 排除常見問題與效能優化
- 實務應用,為你節省數小時的手動工作
快速解答
- 應該使用哪個函式庫? GroupDocs.Comparison for .NET
- 我可以一次比較多個 Word 文件嗎? Yes – add as many target streams as needed.
- 如何在 Word 中標示差異? Use
CompareOptionswith customStyleSettings. - 開發時需要授權嗎? A free trial works for learning; a temporary license removes watermarks.
- 是否支援非同步? Yes – you can wrap the comparison in
Task.Runfor non‑blocking calls.
為什麼要比較多個 Word 文件?
一次比較超過兩個版本,可提供單一、統一的所有變更視圖。這在多位審閱者編輯同一合約,或需要稽核多份提案草稿時尤為有價值。與其處理多個獨立的比較報告,GroupDocs.Comparison 會將每個差異合併到一份文件中,讓你輕鬆辨識新增、刪除與修改。
如何在 Word 文件中標示差異
GroupDocs.Comparison 允許你為插入、刪除或變更的文字定義自訂樣式。透過設定 InsertedItemStyle、DeletedItemStyle 與 ModifiedItemStyle,你可以讓報告符合組織的品牌形象,或僅提升可讀性。我們將示範一個基本範例,將插入的文字以黃色標示。
前置條件
- GroupDocs.Comparison Library(v25.4.0 或更新)– 支援 .NET Framework 4.6.1+ 與 .NET Core 2.0+
- Visual Studio(任何近期版本)
- 基本的 C# 知識 – 你應該能熟練建立主控台應用程式
- 幾個用於測試比較的範例 Word 檔案
讓 GroupDocs.Comparison 快速上手
安裝函式庫(簡易方式)
選項 1:Package Manager Console
Install-Package GroupDocs.Comparison -Version 25.4.0
選項 2:.NET CLI(我個人最愛)
dotnet add package GroupDocs.Comparison --version 25.4.0
授權簡易說明
- 免費試用: 完整功能,僅有輕微浮水印 – 適合學習。
- 臨時授權: 移除示範用浮水印;可向 GroupDocs 申請免費臨時金鑰。
- 正式授權: 前往 GroupDocs Purchase 購買完整授權。
首次比較(Hello World 範例)
using System;
using GroupDocs.Comparison;
namespace DocumentComparisonApp
{
class Program
{
static void Main(string[] args)
{
// Initialize comparer with a source document stream
using (Comparer comparer = new Comparer(File.OpenRead("SOURCE_WORD.docx")))
{
// Add target documents to compare
comparer.Add("TARGET_WORD.docx");
Console.WriteLine("Documents added for comparison.");
}
}
}
}
此程式碼片段建立一個 Comparer 物件,載入來源文件,並加入單一目標文件。可視為設定「前後」比較。
完整實作 – 步驟說明
步驟 1:建立基礎
string documentDirectory = "YOUR_DOCUMENT_DIRECTORY";
using (Comparer comparer = new Comparer(File.OpenRead(System.IO.Path.Combine(documentDirectory, "SOURCE_WORD.docx"))))
{
// We'll build on this foundation
}
發生了什麼? 我們以 串流 而非檔案路徑實例化 Comparer,讓我們能彈性處理儲存在資料庫或透過網路接收的文件。
步驟 2:加入多個目標文件
comparer.Add(File.OpenRead(System.IO.Path.Combine(documentDirectory, "TARGET_WORD.docx")));
comparer.Add(File.OpenRead(System.IO.Path.Combine(documentDirectory, "TARGET2_WORD.docx")));
comparer.Add(File.OpenRead(System.IO.Path.Combine(documentDirectory, "TARGET3_WORD.docx")));
現在你可以在一次執行中 比較多個 Word 文件。GroupDocs.Comparison 會智慧地將所有差異合併成一個結果檔案。
步驟 3:讓差異更醒目(自訂樣式)
CompareOptions compareOptions = new CompareOptions()
{
InsertedItemStyle = new StyleSettings()
{
FontColor = System.Drawing.Color.Yellow // Highlight inserted text in yellow
}
};
自訂樣式 在 Word 中突顯差異,讓利害關係人更易閱讀報告。
步驟 4:執行比較並儲存結果
string outputDirectory = "YOUR_OUTPUT_DIRECTORY";
string outputFileName = System.IO.Path.Combine(outputDirectory, "RESULT_WORD.docx");
comparer.Compare(File.Create(outputFileName), compareOptions);
上述單行程式碼會對所有目標執行比較,並寫入精緻的結果文件。由於使用 File.Create(),你也可以將串流換成資料庫或雲端儲存目的地。
常見問題與解決方式
問題:「找不到檔案」錯誤
string sourcePath = System.IO.Path.Combine(documentDirectory, "SOURCE_WORD.docx");
if (!File.Exists(sourcePath))
{
throw new FileNotFoundException($"Source document not found: {sourcePath}");
}
在開啟串流前務必確認路徑正確。
問題:大型文件的記憶體問題
// Don't do this - keeps all streams in memory
// comparer.Add(File.OpenRead(doc1));
// comparer.Add(File.OpenRead(doc2));
// Do this instead - process one at a time
using (var stream1 = File.OpenRead(doc1))
{
comparer.Add(stream1);
// Stream is disposed automatically here
}
及時釋放串流以降低記憶體使用量。
問題:比較結果異常
CompareOptions options = new CompareOptions()
{
CompareBookmarks = false, // Ignore bookmark differences
CompareComments = false, // Ignore comment differences
CompareFields = false // Ignore field differences
};
調整靈敏度設定,以忽略與審閱無關的元素。
網頁應用程式的非同步比較
public async Task<string> CompareDocumentsAsync(Stream source, Stream[] targets)
{
using (var comparer = new Comparer(source))
{
foreach (var target in targets)
{
comparer.Add(target);
}
// Perform comparison on background thread
return await Task.Run(() =>
{
var output = new MemoryStream();
comparer.Compare(output, compareOptions);
return Convert.ToBase64String(output.ToArray());
});
}
}
將比較包裹於 Task.Run 中,以保持 UI 執行緒的回應性。
效能優化技巧
- 始終釋放串流(
using陳述式)。 - 盡可能順序處理文件。
- 考慮使用非同步模式於 Web API。
- 在高流量情境下使用佇列擴展。
- 保持函式庫為最新版本,以受惠於效能提升。
常見問答
Q: GroupDocs.Comparison 如何處理不同的文件格式?
A: 它支援 Word、PDF、Excel、PowerPoint 等多種格式。API 在各格式間保持一致,因此相同程式碼可用於 PDF、DOCX 等。
Q: 我可以比較版面或結構不同的文件嗎?
A: 可以。引擎以語意比較內容,而非僅逐字比較,因而能優雅處理結構變更。
Q: 若文件受密碼保護該怎麼辦?
A: 開啟串流時可提供密碼,函式庫會為比較解密檔案。
Q: 同時比較的文件數量有上限嗎?
A: 實際上限取決於系統記憶體。在一般開發機上,比較 5‑10 份大型文件通常沒問題。
Q: 如何將此整合至 CI/CD 流程?
A: 將比較邏輯封裝於主控台應用程式或 Web API,然後在建置腳本中呼叫,以自動偵測文件變更。
Q: 函式庫是否支援多語言文件?
A: 當然支援。它能處理從右至左的語言(如阿拉伯文、希伯來文)以及 Unicode 字元。
進一步學習的額外資源
- 文件說明 – Comprehensive API reference and advanced tutorials
- API 參考 – Detailed method and property docs
- 下載中心 – Latest releases and changelogs
- 社群論壇 – 與其他開發者交流,並獲得 GroupDocs 專家的協助
最後更新: 2026-04-14
測試環境: GroupDocs.Comparison 25.4.0 for .NET
作者: GroupDocs