在程式中比較 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 參考文件 以獲取 CompareOptions 與 Settings 的完整說明。
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));
完整程式碼範例:使用 GroupDocs.Comparison 透過 REST 在 C# 中比較 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 憑證、遵守檔案大小限制,並在開發階段使用臨時授權。當您準備好投入生產環境時,請從 定價頁面 購買完整授權,以解鎖無限制使用。
常見問題
如何在 C# 中使用 GroupDocs.Comparison 透過 REST 比較 Excel 檔案?
使用 SDK 上傳兩個 XLSX 檔案,設定CompareOptions,呼叫CompareDocument,並下載產生的檔案。完整的工作流程已在上面的程式碼和 cURL 部分說明。REST API 需要哪種驗證方式?
API 使用 OAuth 2.0 與您的 client‑id 和 client‑secret 取得存取權杖,必須在每個請求的Authorization: Bearer標頭中傳送。我可以自訂比較結果中哪些變更被突顯嗎?
是的。調整Settings物件(例如GenerateSummaryPage、ShowDeletedContent、ShowInsertedContent)以控制差異的顯示方式。臨時授權足以進行測試嗎?
從臨時授權頁面取得的臨時授權可用於開發和測試。對於正式部署,請取得完整授權。
