如何在 .NET 中比較 Excel 檔案
有沒有曾經手動檢查兩個 Excel 檔案之間的差異?無論是追蹤財務報告的變更、比較資料集版本,或是稽核資料完整性,手動比較既耗時又容易出錯。在本指南中,您將學會如何使用 GroupDocs.Comparison for .NET 快速且可靠地比較 Excel 檔案。
快速答案
- 可以使用哪個函式庫? GroupDocs.Comparison for .NET
- 需要多少行程式碼? Less than 10 lines for a basic diff
- 可以比較大型 Excel 活頁簿嗎? Yes – use performance options to handle big files
- 需要授權嗎? A free trial works for testing; a commercial license is required for production
- 是否可以在 Web API 中自動化 Excel 比較? Absolutely – see the ASP.NET controller example
為何以程式方式比較 Excel 檔案?
在進入程式碼之前,先來談談為何自動化的 Excel 比較是顛覆性的:
- 版本控制 – Automatically track changes between document versions without opening files manually
- 資料稽核 – Ensure data consistency across departments and systems
- 品質保證 – Catch discrepancies in reports before they reach stakeholders
- 工作流程自動化 – Integrate comparison logic into larger business processes
GroupDocs.Comparison 函式庫負責所有繁重的工作,您不必擔心解析 Excel 格式或實作複雜的差異演算法。
什麼是 Excel 檔案差異工具?
Excel 檔案差異工具 會比較兩個試算表版本,並突顯新增、刪除與修改的部分。GroupDocs.Comparison 作為一個強大且可程式化的差異工具,直接從您的 .NET 程式碼中運作。
前置條件與設定
您需要的項目
- 開發環境: Visual Studio 2019 或更新版本(VS Code 亦可)
- GroupDocs.Comparison 函式庫: Version 25.4.0 or later
- 基礎知識: Familiarity with C# and file handling in .NET
- 範例檔案: Two Excel files to test with (we’ll show you how to create test scenarios)
安裝 GroupDocs.Comparison for .NET
您有多種安裝方式可選:
選項 1:NuGet 套件管理員主控台
dotnet add package GroupDocs.Comparison --version 25.4.0
選項 2:Visual Studio 套件管理員
- 在 Solution Explorer 中右鍵點擊您的專案
- 選取 Manage NuGet Packages
- 搜尋 GroupDocs.Comparison
- 安裝最新版本
授權選項
在您開始使用時,可先以免費試用版使用 GroupDocs.Comparison。若要投入正式環境,則需要授權:
- 免費試用: Full functionality with evaluation watermarks
- 臨時授權: Request here for testing
- 商業授權: Purchase options for production use
使用 GroupDocs.Comparison 比較 Excel 檔案的方法
現在讓我們建立完整的 Excel 檔案比較解決方案。我們會從簡單開始,並逐步加入更進階的功能。
步驟 1:基本專案設定
首先,建立一個新的 C# 專案,並加入必要的 using 陳述式:
using GroupDocs.Comparison;
using System;
using System.IO;
步驟 2:設定檔案路徑
以乾淨且易於維護的方式設定檔案路徑:
string documentDirectory = "YOUR_DOCUMENT_DIRECTORY";
string resultOutputDirectory = "YOUR_OUTPUT_DIRECTORY";
string sourceFilePath = Path.Combine(documentDirectory, "source_cells.xlsx");
string targetFilePath = Path.Combine(documentDirectory, "target_cells.xlsx");
string resultFilePath = Path.Combine(resultOutputDirectory, "comparison_result.xlsx");
小技巧:使用相對路徑可提升在不同開發環境間的可移植性。像是 Path.Combine("TestData", "source_cells.xlsx") 在大多數情況下都相當好用。
步驟 3:初始化 Comparer
這裡就是魔法發生的地方。Comparer 類別是所有比較操作的入口點:
using (Comparer comparer = new Comparer(sourceFilePath))
{
// Add target file for comparison
comparer.Add(targetFilePath);
}
這裡發生了什麼? Comparer 建構函式會將來源 Excel 檔案載入記憶體,並為比較做準備。using 陳述式確保正確的資源清理——在處理可能很大的 Excel 檔案時這點至關重要。
步驟 4:執行比較
現在進行實際的比較。這非常簡單:
using (Comparer comparer = new Comparer(sourceFilePath))
{
comparer.Add(targetFilePath);
// Compare and save results
comparer.Compare(resultFilePath);
}
在背後,GroupDocs.Comparison 會逐格分析兩個檔案,辨識出:
- 新增的列與欄
- 刪除的內容
- 修改的儲存格值
- 格式變更
- 公式差異
結果會儲存至您指定的輸出檔案,差異會被清楚標示。
步驟 5:完整可執行範例
以下是一個完整、可直接投入生產環境的範例,您可以立即使用:
using GroupDocs.Comparison;
using System;
using System.IO;
class Program
{
static Main()
{
try
{
// Set up file paths
string documentDirectory = @"C:\TestFiles";
string outputDirectory = @"C:\ComparisonResults";
string sourceFile = Path.Combine(documentDirectory, "quarterly_report_v1.xlsx");
string targetFile = Path.Combine(documentDirectory, "quarterly_report_v2.xlsx");
string resultFile = Path.Combine(outputDirectory, "comparison_result.xlsx");
// Ensure output directory exists
Directory.CreateDirectory(outputDirectory);
// Perform comparison
using (Comparer comparer = new Comparer(sourceFile))
{
comparer.Add(targetFile);
comparer.Compare(resultFile);
}
Console.WriteLine($"Comparison complete! Results saved to: {resultFile}");
}
catch (Exception ex)
{
Console.WriteLine($"Error during comparison: {ex.Message}");
}
}
}
自動化 Excel 比較 – 進階設定選項
基本比較已相當強大,但您可能需要對流程有更多控制。以下是一些進階選項。
自訂比較設定
using (Comparer comparer = new Comparer(sourceFilePath))
{
comparer.Add(targetFilePath);
// Configure comparison options
CompareOptions options = new CompareOptions()
{
ShowRevisions = true,
DetectStyleChanges = true,
GenerateSummaryPage = true
};
comparer.Compare(resultFilePath, options);
}
比較多個檔案
需要比較超過兩個檔案嗎?沒問題:
using (Comparer comparer = new Comparer(sourceFilePath))
{
comparer.Add(targetFile1Path);
comparer.Add(targetFile2Path);
comparer.Add(targetFile3Path);
comparer.Compare(resultFilePath);
}
真實案例實作範例
情境 1:財務報表驗證
public class FinancialReportValidator
{
public bool ValidateReportChanges(string previousReport, string currentReport)
{
string comparisonResult = Path.GetTempFileName() + ".xlsx";
using (Comparer comparer = new Comparer(previousReport))
{
comparer.Add(currentReport);
comparer.Compare(comparisonResult);
// Check if there are significant changes
return HasSignificantChanges(comparisonResult);
}
}
private bool HasSignificantChanges(string comparisonFile)
{
// Your business logic here
// For example, check if revenue columns changed by more than 5%
return false;
}
}
情境 2:資料遷移驗證
public class DataMigrationValidator
{
public async Task<bool> VerifyMigration(string sourceData, string migratedData)
{
try
{
string resultPath = $"migration_validation_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx";
using (Comparer comparer = new Comparer(sourceData))
{
comparer.Add(migratedData);
comparer.Compare(resultPath);
}
// Send result to stakeholders
await NotifyStakeholders(resultPath);
return true;
}
catch (Exception ex)
{
// Log error and handle gracefully
Console.WriteLine($"Migration validation failed: {ex.Message}");
return false;
}
}
}
常見問題與解決方案
即使使用簡單的 API,您仍可能遇到一些挑戰。以下是最常見的問題以及解決方式。
問題 1:「檔案正被其他程序使用」
問題:Excel 檔案被其他應用程式鎖定。
解決方案:始終使用 using 陳述式,並確保 Excel 未開啟:
// Good practice
using (Comparer comparer = new Comparer(sourceFilePath))
{
// Your comparison logic
}
// Check if file is in use before comparison
private bool IsFileLocked(string filePath)
{
try
{
using (FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
{
return false;
}
}
catch (IOException)
{
return true;
}
}
問題 2:大型檔案效能
問題:大型 Excel 檔案的比較耗時過長。
解決方案:考慮以下最佳化策略:
// Process files in chunks or limit comparison scope
CompareOptions options = new CompareOptions()
{
// Only compare content, skip formatting for speed
DetectStyleChanges = false,
// Limit comparison to specific ranges if possible
// Note: Range limitation may require custom implementation
};
問題 3:記憶體消耗
問題:在處理大型檔案時,應用程式佔用過多記憶體。
解決方案:實作適當的資源管理:
public void CompareFilesWithMemoryManagement(string source, string target, string output)
{
// Ensure garbage collection
GC.Collect();
GC.WaitForPendingFinalizers();
using (Comparer comparer = new Comparer(source))
{
comparer.Add(target);
comparer.Compare(output);
}
// Force cleanup
GC.Collect();
}
效能最佳化技巧 – 更快偵測 Excel 變更
記憶體管理最佳實踐
- 始終使用
using陳述式 以自動釋放資源 - 依序處理檔案 而非平行處理大型檔案
- 考慮檔案大小限制 – 將巨大的檔案拆分為較小的片段
- 在開發與測試期間監控記憶體使用情況
速度最佳化
// Optimize for speed
CompareOptions fastOptions = new CompareOptions()
{
DetectStyleChanges = false, // Skip formatting comparison
ShowRevisions = false, // Skip revision tracking
GenerateSummaryPage = false // Skip summary generation
};
批次處理策略 – 高效比較大型 Excel 活頁簿
public async Task CompareMultipleFilePairs(List<(string source, string target)> filePairs)
{
foreach (var (source, target) in filePairs)
{
string output = $"comparison_{Path.GetFileNameWithoutExtension(source)}.xlsx";
using (Comparer comparer = new Comparer(source))
{
comparer.Add(target);
comparer.Compare(output);
}
// Small delay to prevent resource exhaustion
await Task.Delay(100);
}
}
與 ASP.NET 應用程式整合 – 透過 API 自動化 Excel 比較
想將 Excel 比較功能加入您的 Web 應用程式嗎?以下是一個基本的控制器範例:
[ApiController]
[Route("api/[controller]")]
public class ExcelComparisonController : ControllerBase
{
[HttpPost("compare")]
public async Task<IActionResult> CompareExcelFiles(IFormFile sourceFile, IFormFile targetFile)
{
if (sourceFile == null || targetFile == null)
return BadRequest("Both source and target files are required.");
try
{
// Save uploaded files temporarily
string tempDir = Path.GetTempPath();
string sourcePath = Path.Combine(tempDir, sourceFile.FileName);
string targetPath = Path.Combine(tempDir, targetFile.FileName);
string resultPath = Path.Combine(tempDir, $"comparison_{Guid.NewGuid()}.xlsx");
using (var sourceStream = new FileStream(sourcePath, FileMode.Create))
{
await sourceFile.CopyToAsync(sourceStream);
}
using (var targetStream = new FileStream(targetPath, FileMode.Create))
{
await targetFile.CopyToAsync(targetStream);
}
// Perform comparison
using (Comparer comparer = new Comparer(sourcePath))
{
comparer.Add(targetPath);
comparer.Compare(resultPath);
}
// Return result file
var resultBytes = await System.IO.File.ReadAllBytesAsync(resultPath);
// Cleanup temporary files
System.IO.File.Delete(sourcePath);
System.IO.File.Delete(targetPath);
System.IO.File.Delete(resultPath);
return File(resultBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "comparison_result.xlsx");
}
catch (Exception ex)
{
return StatusCode(500, $"Comparison failed: {ex.Message}");
}
}
}
何時使用 Excel 檔案比較
在以下情境中,Excel 比較特別有價值:
金融服務
- 季度報告審查 – compare current vs. previous quarter reports
- 預算追蹤 – monitor budget changes across departments
- 稽核準備 – ensure data consistency before external audits
資料管理
- ETL 驗證 – verify data transformations during migration
- 品質保證 – ensure imported data matches source systems
- 版本控制 – track changes in master data files
商業智慧
- 報告驗證 – compare automated reports with manual calculations
- 資料對帳 – match data between different systems
- 變更追蹤 – monitor KPI changes over time
常見問答
Q: GroupDocs.Comparison 能處理的最大檔案大小是多少?
A: 該函式庫可處理高達數百 MB 的檔案,但效能取決於可用記憶體。對於超過 50 MB 的檔案,建議採用分塊處理或串流方式。
Q: 能比較受密碼保護的 Excel 檔案嗎?
A: 可以,但您需要在比較過程中提供密碼。函式庫支援使用正確憑證的加密 Excel 檔案。
Q: 對於含有複雜公式的 Excel 檔案,比較的準確度如何?
A: GroupDocs.Comparison 能精確偵測公式變更,包括儲存格參照與函式修改。它將公式視為內容變更並相應標示。
Q: 我可以自訂比較結果的視覺輸出嗎?
A: 該函式庫提供多種內建的突顯樣式。若需自訂樣式,您可以在輸出檔案後處理,或探索 API 的樣式選項。
Q: 有辦法只比較 Excel 檔案中的特定工作表嗎?
A: 雖然函式庫預設會比較整個檔案,您可以在比較前先預處理檔案以抽取特定工作表,或在比較後過濾結果以保留相關變更。
Q: GroupDocs.Comparison 如何偵測 Excel 變更?
A: 它執行逐格差異比較,檢查值、公式、格式以及結構變更(如新增或刪除列/欄)。
Q: 這個工具能有效處理非常大的 Excel 活頁簿嗎?
A: 可以——透過停用樣式偵測 (DetectStyleChanges = false) 並使用批次處理,即可有效比較大型 Excel 檔案。
其他資源
- 文件說明: GroupDocs Comparison .NET Documentation
- API 參考: GroupDocs Comparison .NET API Reference
- 下載: GroupDocs Releases for .NET
- 購買授權: Buy GroupDocs License
- 免費試用: GroupDocs Free Trial
- 臨時授權: Request Temporary License
- 支援論壇: GroupDocs Support Community
最後更新: 2026-04-10
測試版本: GroupDocs.Comparison 25.4.0
作者: GroupDocs