Excel ワークシートをプログラムで比較することは、データの変更を検出したり、改訂を監査したり、レポートを生成したりする必要がある場合に頻繁に求められる要件です。 GroupDocs.Comparison Cloud SDK for .NET は、REST ベースの強力なライブラリを提供し、C# で REST を介して Excel ファイルを簡単に比較できるようにします。このガイドでは、ファイルのアップロード、比較オプションの設定、比較の実行、結果の取得までを、C# コードと cURL の例とともに順を追って説明します。

REST を使用した C# での Excel ファイル比較(5 ステップ)

  1. ソースの XLSX ファイルをアップロード: StorageApi.UploadFile メソッドを使用して sample1.xlsx をクラウドストレージに配置します。
using (var stream1 = new FileStream(localFilePath1, FileMode.Open, FileAccess.Read))
{
    var uploadRequest1 = new UploadFileRequest(remoteFilePath1, stream1);
    storageApi.UploadFile(uploadRequest1);
}
  1. ターゲットの XLSX ファイルをアップロード: sample2.xlsx についても同様にアップロードします。
using (var stream2 = new FileStream(localFilePath2, FileMode.Open, FileAccess.Read))
{
    var uploadRequest2 = new UploadFileRequest(remoteFilePath2, stream2);
    storageApi.UploadFile(uploadRequest2);
}
  1. 比較オプションを構成: 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
    }
};

CompareOptions と Settings の完全な API リファレンスは、API Reference を参照してください。

  1. 比較を実行: CompareApi.CompareDocument を CompareDocumentRequest とともに呼び出します。
var compareRequest = new CompareDocumentRequest(compareOptions);
var compareResult = compareApi.CompareDocument(compareRequest);
  1. 結果をダウンロードしてクリーンアップ: 生成された 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));

完全なコード例: C# で REST を使用して Excel ファイルを比較 (GroupDocs.Comparison 使用)

この例では、2つの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);
        }
    }
}

注意: このコード例はコア機能を示しています。プロジェクトで使用する前に、ファイルパスや構成値を実際の環境に合わせて更新し、すべての必須依存関係が正しくインストールされていることを確認し、開発環境で徹底的にテストしてください。問題が発生した場合は、公式ドキュメント または サポートチーム にお問い合わせください。

Calling the Comparison REST API with cURL

以下は、C#コードを書かずに同じ操作を実行するcURLワークフローです。プレースホルダーの値を実際の認証情報とファイル名に置き換えてください。

  1. アクセストークンを取得

    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"}'
    
  2. ソース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"
    
  3. ターゲット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"
    
  4. 比較を実行

    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
               }
             }'
    
  5. 結果をダウンロード

    curl -X GET "https://api.groupdocs.cloud/v2.0/storage/file/result.xlsx" \
         -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
         -o result.xlsx
    

エンドポイントとパラメータの完全な一覧については、公式 API ドキュメントをご覧ください。

前提条件とセットアップ

  1. .NET パッケージを NuGet でインストールします:
dotnet add package GroupDocs.Comparison-Cloud
  1. 有効な GroupDocs Cloud アカウントを持ち、ダッシュボードから Client Id と Client Secret を取得してください。
  2. .NET 6.0 以降が必要です。

最新のライブラリはダウンロードページからダウンロードできます。

比較設定の微調整

CompareOptions の内部にある Settings オブジェクトは、出力を制御できます。一般的な調整項目は次のとおりです:

  • GenerateSummaryPage - 変更統計を含むサマリーシートを追加します。
  • ShowDeletedContent - 削除されたセルをハイライト表示します。
  • ShowInsertedContent - 新しく追加されたセルをハイライト表示します。

これらのプロパティは上記のコード例ですでに使用されています。Password 保護や StyleChangeDetection などの追加オプションについては、API リファレンス を参照してください。

結論

C# で REST を使用して Excel ファイルを比較することは、GroupDocs.Comparison Cloud SDK for .NET を使用すれば簡単になります。手順に従うことで、XLSX ファイルをアップロードし、詳細な比較設定を構成し、比較を実行し、差分が明確にマークされた結果のワークブックを取得できます。API 資格情報を保護し、ファイル サイズ制限を守り、開発には一時ライセンスを使用することを忘れないでください。製品環境の準備ができたら、価格ページ からフルライセンスを購入し、無制限の使用を解放しましょう。

FAQ

  • C# の GroupDocs.Comparison を使用して REST 経由で Excel ファイルを比較するにはどうすればよいですか?
    SDK を使用して 2 つの XLSX ファイルをアップロードし、CompareOptions を設定し、CompareDocument を呼び出して、結果のファイルをダウンロードします。完全なワークフローは上記のコードと cURL セクションに示されています。

  • REST API が必要とする認証方法は何ですか?
    API は OAuth 2.0 を使用し、クライアント‑id とクライアント‑secret でアクセストークンを取得します。このトークンは、すべてのリクエストで Authorization: Bearer ヘッダーに含めて送信する必要があります。

  • 比較結果でハイライトされる変更をカスタマイズできますか?
    はい。Settings オブジェクトを調整します(例: GenerateSummaryPage、ShowDeletedContent、ShowInsertedContent)ことで、差分の表示を制御できます。

  • テストに一時ライセンスで十分ですか?
    開発およびテストには、一時ライセンスページの一時ライセンスが機能します。 本番環境へのデプロイには、フルライセンスを取得してください。

さらに読む