如何在 .NET 中比较 Excel 文件

是否曾经手动检查两个 Excel 文件之间的差异?无论是跟踪财务报告的变更、比较数据集版本,还是审计数据完整性,手动比较既耗时又容易出错。在本指南中,您将学习如何使用 GroupDocs.Comparison for .NET 快速且可靠地比较 Excel 文件

快速答案

  • 可以使用哪个库? GroupDocs.Comparison for .NET
  • 需要多少行代码? 基本差异少于 10 行
  • 我可以比较大型 Excel 工作簿吗? 是 – 使用性能选项来处理大文件
  • 我需要许可证吗? 免费试用可用于测试;生产环境需要商业许可证
  • 是否可以在 Web API 中自动化 Excel 比较? 当然 – 请参阅 ASP.NET 控制器示例

为什么要以编程方式比较 Excel 文件?

在进入代码之前,让我们谈谈为何自动化 Excel 比较是一个改变游戏规则的技术:

  • 版本控制 – 自动跟踪文档版本之间的更改,无需手动打开文件
  • 数据审计 – 确保跨部门和系统的数据一致性
  • 质量保证 – 在报告到达利益相关者之前捕获差异
  • 工作流自动化 – 将比较逻辑集成到更大的业务流程中

GroupDocs.Comparison 库处理所有繁重的工作,您无需担心解析 Excel 格式或实现复杂的差异算法。

什么是 Excel 文件差异工具?

一个 excel file diff tool(Excel 文件差异工具)比较两个电子表格版本并突出显示新增、删除和修改。GroupDocs.Comparison 充当强大的编程式差异工具,可直接从您的 .NET 代码中工作。

前置条件和设置

您需要的内容

  • 开发环境:Visual Studio 2019 或更高版本(VS Code 也可使用)
  • GroupDocs.Comparison 库:版本 25.4.0 或更高
  • 基础知识:熟悉 C# 和 .NET 中的文件处理
  • 示例文件:用于测试的两个 Excel 文件(我们将展示如何创建测试场景)

安装 GroupDocs.Comparison for .NET

您有多种安装选项:

选项 1:NuGet 包管理器控制台

dotnet add package GroupDocs.Comparison --version 25.4.0

选项 2:Visual Studio 包管理器

  1. 在 Solution Explorer 中右键单击您的项目
  2. 选择 Manage NuGet Packages
  3. 搜索 GroupDocs.Comparison
  4. 安装最新版本

许可选项

在您入门时,可以使用免费试用的 GroupDocs.Comparison。生产环境使用时,需要许可证:

  • 免费试用:完整功能并带有评估水印
  • 临时许可证:用于测试的 Request here
  • 商业许可证:用于生产的 Purchase options

如何使用 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 更改

内存管理最佳实践

  1. 始终使用 using 语句 以自动释放资源
  2. 顺序处理文件 而非并行处理,以适用于大文件
  3. 考虑文件大小限制 – 将巨大的文件拆分为更小的块
  4. 在开发和测试期间监控内存使用

Speed Optimization

// 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 比较

想在您的 Web 应用程序中添加 Excel 比较功能吗?以下是一个基本的控制器示例:

[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 比较在以下场景中特别有价值:

金融服务

  • 季度报告审查 – 比较本季度与上季度的报告
  • 预算跟踪 – 监控各部门的预算变动
  • 审计准备 – 在外部审计前确保数据一致性

数据管理

  • ETL 验证 – 验证迁移期间的数据转换
  • 质量保证 – 确保导入的数据与源系统匹配
  • 版本控制 – 跟踪主数据文件的更改

商业智能

  • 报告验证 – 将自动化报告与手动计算进行比较
  • 数据对账 – 匹配不同系统之间的数据
  • 变更跟踪 – 随时间监控关键绩效指标的变化

常见问题

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 文件。

附加资源


最后更新: 2026-04-10
测试版本: GroupDocs.Comparison 25.4.0
作者: GroupDocs