在编程中比较 Excel 工作表是一个常见需求,当您需要检测数据更改、审计修订或生成报告时。 GroupDocs.Comparison Cloud SDK for .NET 提供了一个强大的基于 REST 的库,使得在 C# 中通过 REST 比较 Excel 文件变得轻而易举。本指南将带您完成文件上传、配置比较选项、执行比较以及检索结果的全过程,并提供 C# 代码和 cURL 示例。
使用 REST 在 C# 中比较 Excel 文件的 5 步骤
- 上传源 XLSX 文件:使用
StorageApi.UploadFile方法将sample1.xlsx放置到云存储中。
using (var stream1 = new FileStream(localFilePath1, FileMode.Open, FileAccess.Read))
{
var uploadRequest1 = new UploadFileRequest(remoteFilePath1, stream1);
storageApi.UploadFile(uploadRequest1);
}
- 上传目标 XLSX 文件:对
sample2.xlsx重复上传操作。
using (var stream2 = new FileStream(localFilePath2, FileMode.Open, FileAccess.Read))
{
var uploadRequest2 = new UploadFileRequest(remoteFilePath2, stream2);
storageApi.UploadFile(uploadRequest2);
}
- 配置比较选项:创建
CompareOptions对象,设置源文件和目标文件路径,定义输出路径,并调整诸如GenerateSummaryPage等设置。
var compareOptions = new CompareOptions
{
SourceFile = new FileInfo { FilePath = remoteFilePath1 },
TargetFiles = new List<FileInfo>
{
new FileInfo { FilePath = remoteFilePath2 }
},
OutputPath = remoteResultPath,
Settings = new Settings
{
GenerateSummaryPage = true,
ShowDeletedContent = true,
ShowInsertedContent = true
}
};
请在 API Reference 查看 CompareOptions 和 Settings 的完整 API 参考。
4. 执行比较:使用 CompareDocumentRequest 调用 CompareApi.CompareDocument。
var compareRequest = new CompareDocumentRequest(compareOptions);
var compareResult = compareApi.CompareDocument(compareRequest);
- 下载结果并清理:获取生成的
result.xlsx,将其保存到本地,并可选择删除远程文件。
var downloadRequest = new DownloadFileRequest(compareResult.Path);
using (var resultStream = storageApi.DownloadFile(downloadRequest))
using (var fileStream = new FileStream(localResultPath, FileMode.Create, FileAccess.Write))
{
resultStream.CopyTo(fileStream);
}
// Clean up
storageApi.DeleteFile(new DeleteFileRequest(remoteFilePath1));
storageApi.DeleteFile(new DeleteFileRequest(remoteFilePath2));
storageApi.DeleteFile(new DeleteFileRequest(remoteResultPath));
完整代码示例:通过 REST 在 C# 中使用 GroupDocs.Comparison 比较 Excel 文件
此示例演示了如何上传两个 XLSX 文件,进行比较,并下载带有高亮显示的结果。
using System;
using System.Collections.Generic;
using System.IO;
using GroupDocs.Comparison.Cloud.Sdk.Api;
using GroupDocs.Comparison.Cloud.Sdk.Client;
using GroupDocs.Comparison.Cloud.Sdk.Model;
using GroupDocs.Comparison.Cloud.Sdk.Model.Requests;
namespace CompareExcelFilesExample
{
class Program
{
static void Main(string[] args)
{
// Configuration – replace with your actual credentials
var config = new Configuration
{
ClientId = "YOUR_CLIENT_ID",
ClientSecret = "YOUR_CLIENT_SECRET"
};
// APIs
var compareApi = new CompareApi(config);
var storageApi = new StorageApi(config);
// Local and remote file names
string localFilePath1 = "sample1.xlsx";
string localFilePath2 = "sample2.xlsx";
string remoteFilePath1 = "sample1.xlsx";
string remoteFilePath2 = "sample2.xlsx";
string remoteResultPath = "result.xlsx";
string localResultPath = "result.xlsx";
// Upload first Excel file
using (var stream1 = new FileStream(localFilePath1, FileMode.Open, FileAccess.Read))
{
var uploadRequest1 = new UploadFileRequest(remoteFilePath1, stream1);
storageApi.UploadFile(uploadRequest1);
}
// Upload second Excel file
using (var stream2 = new FileStream(localFilePath2, FileMode.Open, FileAccess.Read))
{
var uploadRequest2 = new UploadFileRequest(remoteFilePath2, stream2);
storageApi.UploadFile(uploadRequest2);
}
// Prepare comparison options
var compareOptions = new CompareOptions
{
SourceFile = new FileInfo { FilePath = remoteFilePath1 },
TargetFiles = new List<FileInfo>
{
new FileInfo { FilePath = remoteFilePath2 }
},
OutputPath = remoteResultPath,
Settings = new Settings
{
// Example: show changes in a new sheet
GenerateSummaryPage = true,
ShowDeletedContent = true,
ShowInsertedContent = true
}
};
// Execute comparison
var compareRequest = new CompareDocumentRequest(compareOptions);
var compareResult = compareApi.CompareDocument(compareRequest);
// Download the result file
var downloadRequest = new DownloadFileRequest(compareResult.Path);
using (var resultStream = storageApi.DownloadFile(downloadRequest))
using (var fileStream = new FileStream(localResultPath, FileMode.Create, FileAccess.Write))
{
resultStream.CopyTo(fileStream);
}
// Optional: clean up remote files
storageApi.DeleteFile(new DeleteFileRequest(remoteFilePath1));
storageApi.DeleteFile(new DeleteFileRequest(remoteFilePath2));
storageApi.DeleteFile(new DeleteFileRequest(remoteResultPath));
Console.WriteLine("Comparison completed. Result saved to " + localResultPath);
}
}
}
注意: 此代码示例演示了核心功能。在将其用于您的项目之前,请确保更新所有文件路径和配置值以匹配实际环境,验证已正确安装所有必需的依赖项,并在开发环境中进行彻底测试。如果遇到任何问题,请参阅官方文档或联系支持团队获取帮助。
使用 cURL 调用 Comparison REST API
以下是一个 cURL 工作流,它执行相同的操作而无需编写 C# 代码。将占位符值替换为您的实际凭据和文件名。
- 获取访问令牌
curl -X POST "https://api.groupdocs.cloud/v2.0/oauth2/token" \
-H "Content-Type: application/json" \
-d '{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'
- 上传源 XLSX 文件
curl -X PUT "https://api.groupdocs.cloud/v2.0/storage/file/sample1.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" \
--data-binary "@sample1.xlsx"
- 上传目标 XLSX 文件
curl -X PUT "https://api.groupdocs.cloud/v2.0/storage/file/sample2.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" \
--data-binary "@sample2.xlsx"
- 运行比较
curl -X POST "https://api.groupdocs.cloud/v2.0/comparison/compare" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"source_file": {"file_path": "sample1.xlsx"},
"target_files": [{"file_path": "sample2.xlsx"}],
"output_path": "result.xlsx",
"settings": {
"generate_summary_page": true,
"show_deleted_content": true,
"show_inserted_content": true
}
}'
- 下载结果
curl -X GET "https://api.groupdocs.cloud/v2.0/storage/file/result.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o result.xlsx
要获取完整的端点和参数列表,请参阅官方 API 文档。
前置条件和设置
- 通过 NuGet 安装 .NET 包:
dotnet add package GroupDocs.Comparison-Cloud
- 确保您拥有有效的 GroupDocs Cloud 帐户,并从仪表板获取 Client Id 和 Client Secret。
- 需要 .NET 6.0 或更高版本。
您可以从下载页面下载最新的库。
微调比较设置
在 CompareOptions 中的 Settings 对象允许您控制输出。常见的调整包括:
- GenerateSummaryPage - 添加一个包含更改统计信息的摘要工作表。
- ShowDeletedContent - 突出显示已删除的单元格。
- ShowInsertedContent - 突出显示新添加的单元格。
这些属性已在上面的代码示例中使用。有关 Password 保护或 StyleChangeDetection 等其他选项,请参阅 API 参考。
结论
通过 REST 在 C# 中比较 Excel 文件变得简单,只需使用 GroupDocs.Comparison Cloud SDK for .NET。按照步骤操作,您可以上传 XLSX 文件,配置详细的比较设置,执行比较,并获取清晰标记差异的结果工作簿。请记住保护您的 API 凭据,遵守文件大小限制,并在开发期间使用临时许可证。准备好投入生产时,可从 定价页面 购买完整许可证,以解锁无限使用。
FAQs
如何在 C# 中通过 REST 使用 GroupDocs.Comparison 比较 Excel 文件?
使用 SDK 上传两个 XLSX 文件,设置CompareOptions,调用CompareDocument,并下载生成的文件。完整的工作流程在上面的代码和 cURL 部分中有说明。REST API 需要哪种身份验证方法?
API 使用 OAuth 2.0 与您的 client‑id 和 client‑secret 获取访问令牌,必须在每个请求的Authorization: Bearer头中发送该令牌。我可以自定义比较结果中突出显示的更改吗?
可以。调整Settings对象(例如GenerateSummaryPage、ShowDeletedContent、ShowInsertedContent)以控制差异的显示方式。临时许可证足以用于测试吗?
来自临时许可证页面的临时许可证可用于开发和测试。对于生产部署,请获取完整许可证。
