分析試算表版本之間的變更是財務、報告與資料驗證工作流程中的常規任務。 GroupDocs.Comparison Cloud SDK for .NET 提供強大的 API,使得在 .NET 中輕鬆比較 Excel 檔案,無需手動檢查。 在本指南中,您將了解如何設定庫、上傳工作簿、從 C# 呼叫 REST 端點、取得詳細的變更資訊,並微調比較選項以獲得精確的結果。
開始之前:先決條件與安裝
開始之前,請確保您具備以下條件:
- .NET 6.0 或更新版本已安裝。
- IDE,例如 Visual Studio 2022。
- 具備 ClientId 和 ClientSecret 的 GroupDocs Cloud 帳戶。
- 取得可上傳 Excel 檔案的 GroupDocs Cloud 儲存空間存取權限。
通過 NuGet 安裝 SDK:
dotnet add package GroupDocs.Comparison-Cloud
從release page下載最新的套件。安裝完成後,請在程式碼中加入您的憑證,如下所示。現在您已準備好開始比較工作流程。
在 .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 或背景工作中。請記得為生產環境取得適當的授權;臨時授權可從 授權頁面 獲得,完整授權細節列於產品網站。立即開始自動化您的 Excel 差異工作流程,消除人工檢查錯誤。
常見問題
如何在 .NET 中比較 Excel 檔案而不需要編寫大量程式碼?
SDK 抽象了繁重的工作。您只需配置憑證,指向兩個 XLSX 檔案,然後呼叫compareApi.Compare。該函式庫會返回差異檔案和變更物件清單。差異結果使用什麼格式?
預設情況下,結果會儲存為 XLSX 工作簿,保留原始佈局,同時為刪除、插入和樣式變更套用視覺標記。是否可以比較受保護的 Excel 工作簿?
是的。請在FileInfo物件的Password屬性中提供密碼。API 會在執行比較之前解鎖工作簿。我需要開發和測試的授權嗎?
您可以從授權頁面獲取臨時授權。對於正式部署,您必須根據產品頁面說明購買完整授權。
