分析电子表格版本之间的更改是财务、报告和数据验证工作流中的常规任务。 GroupDocs.Comparison Cloud SDK for .NET 提供了强大的 API,使在 .NET 中轻松比较 Excel 文件,无需手动检查。在本指南中,您将了解如何设置库、上传工作簿、从 C# 调用 REST 端点、检索详细的更改信息,并微调比较选项以获得精确的结果。
开始之前:先决条件和安装
在开始之前,请确保您具备以下条件:
- .NET 6.0 或更高版本已安装。
- 如 Visual Studio 2022 等 IDE。
- 拥有 ClientId 和 ClientSecret 的 GroupDocs Cloud 帐户。
- 能够访问将上传 Excel 文件的 GroupDocs Cloud 存储。
通过 NuGet 安装 SDK:
dotnet add package GroupDocs.Comparison-Cloud
从发布页面下载最新的包。安装后,按照后面的示例在代码中添加您的凭据。现在您已经准备好开始比较工作流。
.NET 中比较 Excel 文件的分步指南
以下步骤演示如何在 .NET 中使用 SDK 比较 Excel 文件。
步骤 1:加载源文档和目标文档
使用您的凭据创建配置对象并实例化 CompareApi。
var config = new Configuration
{
ClientId = "YOUR_CLIENT_ID",
ClientSecret = "YOUR_CLIENT_SECRET"
};
var compareApi = new CompareApi(config);
第2步:准备 Excel 工作簿
定义指向存储在 GroupDocs Cloud 存储中的源和目标 XLSX 文件的 FileInfo 对象。
var sourceFile = new FileInfo { FilePath = "input/source.xlsx" };
var targetFile = new FileInfo { FilePath = "input/target.xlsx" };
第3步:设置比较选项和设置
配置 CompareOptions 对象,包括详细的更改设置。
var compareOptions = new CompareOptions
{
SourceFile = sourceFile,
TargetFiles = new List<FileInfo> { targetFile },
OutputPath = "output/diff_result.xlsx",
Settings = new Settings
{
ShowDeletedContent = true,
ShowInsertedContent = true,
ShowStyleChanges = true,
GenerateSummaryPage = true
}
};
有关完整的设置列表,请参阅 API 参考。
第4步:执行比较并处理结果
调用 Compare 方法并读取基本结果信息。
var compareResult = compareApi.Compare(compareOptions);
Console.WriteLine($"Comparison completed. Result file: {compareResult.Path}");
Console.WriteLine($"Number of changes detected: {compareResult.Changes?.Count ?? 0}");
步骤 5:检索详细更改信息
如果您需要更细粒度的报告,请使用 GetChanges 请求。
var changesRequest = new GetChangesRequest
{
SourceFile = sourceFile,
TargetFile = targetFile,
OutputPath = "output/detailed_changes.json"
};
var detailedChanges = compareApi.GetChanges(changesRequest);
Console.WriteLine($"Detailed changes saved to: {detailedChanges.Path}");
通过这些步骤,您已经完全自动化了在 .NET 中比较 Excel 文件的过程。
编程比较 Excel 文件 - 完整代码示例
以下代码演示了从配置到结果处理的完整工作流。
using System;
using System.Collections.Generic;
using GroupDocs.Comparison.Cloud.Sdk.Api;
using GroupDocs.Comparison.Cloud.Sdk.Client;
using GroupDocs.Comparison.Cloud.Sdk.Model;
namespace CompareExcelDemo
{
class Program
{
static void Main(string[] args)
{
// Set up API credentials (replace with your actual credentials)
var config = new Configuration
{
ClientId = "YOUR_CLIENT_ID",
ClientSecret = "YOUR_CLIENT_SECRET"
};
// Initialize Compare API
var compareApi = new CompareApi(config);
// Prepare source and target Excel files (must be uploaded to GroupDocs Cloud storage beforehand)
var sourceFile = new FileInfo { FilePath = "input/source.xlsx" };
var targetFile = new FileInfo { FilePath = "input/target.xlsx" };
// Define comparison options
var compareOptions = new CompareOptions
{
SourceFile = sourceFile,
TargetFiles = new List<FileInfo> { targetFile },
OutputPath = "output/diff_result.xlsx",
// Optional: specify that we want detailed changes for worksheets
Settings = new Settings
{
ShowDeletedContent = true,
ShowInsertedContent = true,
ShowStyleChanges = true,
GenerateSummaryPage = true
}
};
try
{
// Call the Compare API
var compareResult = compareApi.Compare(compareOptions);
// Output basic result information
Console.WriteLine($"Comparison completed. Result file: {compareResult.Path}");
Console.WriteLine($"Number of changes detected: {compareResult.Changes?.Count ?? 0}");
// Iterate through change details
if (compareResult.Changes != null)
{
foreach (var change in compareResult.Changes)
{
Console.WriteLine("--------------------------------------------------");
Console.WriteLine($"Change Type : {change.ChangeType}");
Console.WriteLine($"Worksheet : {change.Worksheet}");
Console.WriteLine($"Cell Address : {change.CellAddress}");
Console.WriteLine($"Old Value : {change.OldValue}");
Console.WriteLine($"New Value : {change.NewValue}");
Console.WriteLine($"Is New Row : {change.IsNewRow}");
Console.WriteLine($"Is New Column : {change.IsNewColumn}");
}
}
// Retrieve detailed changes via GetChanges (optional)
var changesRequest = new GetChangesRequest
{
SourceFile = sourceFile,
TargetFile = targetFile,
OutputPath = "output/detailed_changes.json"
};
var detailedChanges = compareApi.GetChanges(changesRequest);
Console.WriteLine($"Detailed changes saved to: {detailedChanges.Path}");
}
catch (ApiException apiEx)
{
Console.WriteLine($"API error: {apiEx.ErrorCode} - {apiEx.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
}
}
注意: 此代码示例演示了核心功能。在将其用于项目之前,请确保更新所有文件路径和配置值以匹配实际环境,验证已正确安装所有必需的依赖项,并在开发环境中彻底测试。如果遇到任何问题,请参阅官方文档或联系支持团队获取帮助。
使用 cURL 通过 REST 执行 Excel 比较
您可以通过直接调用 REST API 来实现相同的结果,而无需编写 C# 代码。
1. 进行身份验证并获取访问令牌
curl -X POST "https://api.groupdocs.cloud/v1.0/oauth2/token" \
-H "Content-Type: application/json" \
-d '{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'
2. 上传源工作簿
curl -X PUT "https://api.groupdocs.cloud/v1.0/storage/file?path=input/source.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-T "./source.xlsx"
3. 上传目标工作簿
curl -X PUT "https://api.groupdocs.cloud/v1.0/storage/file?path=input/target.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-T "./target.xlsx"
4. 执行比较
curl -X POST "https://api.groupdocs.cloud/v1.0/comparison/compare" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"source_file": {"file_path":"input/source.xlsx"},
"target_files": [{"file_path":"input/target.xlsx"}],
"output_path":"output/diff_result.xlsx",
"settings": {
"show_deleted_content": true,
"show_inserted_content": true,
"show_style_changes": true,
"generate_summary_page": true
}
}'
5. 下载生成的差异工作簿
curl -X GET "https://api.groupdocs.cloud/v1.0/storage/file?path=output/diff_result.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o diff_result.xlsx
有关完整的参数列表,请参阅官方 API 文档。
结论
使用 GroupDocs.Comparison Cloud SDK for .NET,您可以轻松比较 .NET 中的 Excel 文件,并获取详细的差异工作簿,突出显示每一次修改。SDK 的基于 REST 的架构让您能够将电子表格比较集成到任何 C# 服务、Web API 或后台任务中,几乎不费力。请记得为生产环境获取合适的许可证;临时许可证可在 license page 获取,完整的授权详情列在产品网站上。立即开始自动化您的 Excel 差异工作流,消除人工检查错误。
FAQs
如何在 .NET 中比较 Excel 文件而无需编写大量代码?
SDK 抽象了繁重的工作。您只需配置凭据,指向两个 XLSX 文件,然后调用compareApi.Compare。库会返回一个差异文件和一个更改对象列表。Diff 结果使用什么格式?
默认情况下,结果保存为 XLSX 工作簿,保留原始布局,同时为删除、插入和样式更改应用可视标记。是否可以比较受保护的 Excel 工作簿?
是的。在FileInfo对象的Password属性中提供密码。API 将在执行比较之前解锁工作簿。我需要为开发和测试获取许可证吗?
可以从许可证页面获取临时许可证。对于生产部署,您必须按照产品页面的说明购买完整许可证。
