Excel ワークブック内の機密データを隠すことは、社内またはパートナーとレポートを共有する企業にとって一般的な要件です。 GroupDocs.Merger Cloud SDK for Java は、開発者がシンプルな REST API を通じて XLSX ファイルにパスワード保護を追加できるようにします。このガイドでは、Java で REST を使用した Excel のパスワード保護 の実装方法を学び、完全な Java のサンプルを確認し、クラウド統合のための同等の cURL コマンドを実行します。

前提条件とセットアップ

開始する前に、以下のものが揃っていることを確認してください:

  • Java 8 以上がインストールされていること。
  • 依存関係管理のための Maven または Gradle。
  • client IDclient secret を持つ GroupDocs Cloud アカウント。
  • Excel ファイルが保存されるクラウドストレージへのアクセス。

Maven 依存関係を pom.xml に追加します(または同等の Gradle スニペット):

<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-merger-cloud</artifactId>
    <version>25.11</version>
</dependency>

最新の JAR は ダウンロードページ から取得できます。依存関係を追加した後、資格情報で構成オブジェクトを作成します:

String clientId = "YOUR_CLIENT_ID";
String clientSecret = "YOUR_CLIENT_SECRET";

Configuration config = new Configuration(clientId, clientSecret);
MergerApi mergerApi = new MergerApi(config);

クライアントの準備ができたら、実際のパスワード保護ワークフローに進むことができます。

JavaでRESTを使用したExcelのパスワード保護:ステップバイステップの手順

ステップ 1: ソース ドキュメントの読み込み

クラウドストレージ内のソースXLSXファイルの場所を定義します。

FileInfo inputFile = new FileInfo();
inputFile.setFilePath("input.xlsx");   // source Excel file

ステップ 2: 宛先パスを設定

保護されたワークブックの保存場所を指定します。

String outputPath = "output_protected.xlsx"; // destination file

ステップ 3: パスワード保護オプションを設定

ProtectOptions オブジェクトを作成し、ファイル情報、出力パス、および希望するパスワードを添付します。

ProtectOptions protectOptions = new ProtectOptions();
protectOptions.setFileInfo(inputFile);
protectOptions.setOutputPath(outputPath);
protectOptions.setPassword("MySecretPassword"); // desired password

ステップ 4: Protect Document リクエストを作成

オプションを ProtectDocumentRequest でラップします。

ProtectDocumentRequest request = new ProtectDocumentRequest(protectOptions);

ステップ 5: リクエストを実行し、レスポンスを処理する

API メソッドを呼び出し、エラーを処理します。

try {
    mergerApi.protectDocument(request);
    System.out.println("Excel file has been password protected successfully.");
    System.out.println("Protected file stored at: " + outputPath);
} catch (ApiException e) {
    System.err.println("Error while protecting the Excel file:");
    System.err.println("Status Code: " + e.getCode());
    System.err.println("Message: " + e.getMessage());
}

各クラスの詳細については、API リファレンス を参照してください。

Java で REST を使用した Excel のパスワード保護 完全動作例

以下のコードは、開始から完了までの全プロセスを示しています。

import com.groupdocs.merger.cloud.ApiException;
import com.groupdocs.merger.cloud.Configuration;
import com.groupdocs.merger.cloud.api.MergerApi;
import com.groupdocs.merger.cloud.model.FileInfo;
import com.groupdocs.merger.cloud.model.ProtectOptions;
import com.groupdocs.merger.cloud.model.requests.ProtectDocumentRequest;

public class ProtectExcelExample {
    public static void main(String[] args) {
        // Replace with your actual client credentials
        String clientId = "YOUR_CLIENT_ID";
        String clientSecret = "YOUR_CLIENT_SECRET";

// Initialize the API configuration
        Configuration config = new Configuration(clientId, clientSecret);
        MergerApi mergerApi = new MergerApi(config);

// Define input and output file locations (relative to the storage root)
        FileInfo inputFile = new FileInfo();
        inputFile.setFilePath("input.xlsx");          // source Excel file
        String outputPath = "output_protected.xlsx";  // destination file

// Set password protection options
        ProtectOptions protectOptions = new ProtectOptions();
        protectOptions.setFileInfo(inputFile);
        protectOptions.setOutputPath(outputPath);
        protectOptions.setPassword("MySecretPassword"); // desired password

// Build the request
        ProtectDocumentRequest request = new ProtectDocumentRequest(protectOptions);

try {
            // Execute the password protection operation
            mergerApi.protectDocument(request);
            System.out.println("Excel file has been password protected successfully.");
            System.out.println("Protected file stored at: " + outputPath);
        } catch (ApiException e) {
            System.err.println("Error while protecting the Excel file:");
            System.err.println("Status Code: " + e.getCode());
            System.err.println("Message: " + e.getMessage());
        }
    }
}

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

cURL を使用した REST API で Excel ワークブックを保護する

以下は、REST エンドポイントに直接対して同じパスワード保護操作を実行する cURL コマンドのセットです。

1. 認証してアクセストークンを取得する

curl -X POST "https://api.groupdocs.cloud/v2.0/auth/token" \
     -H "Content-Type: application/json" \
     -d '{
           "client_id": "YOUR_CLIENT_ID",
           "client_secret": "YOUR_CLIENT_SECRET"
         }'

レスポンスには、以降の呼び出しで使用する access_token が含まれています。

2. ソースXLSXファイルをアップロードする

curl -X PUT "https://api.groupdocs.cloud/v2.0/storage/file/input.xlsx" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/octet-stream" \
     --data-binary @input.xlsx

3. パスワード保護のリクエスト

curl -X POST "https://api.groupdocs.cloud/v2.0/merger/protect" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{
           "fileInfo": { "filePath": "input.xlsx" },
           "outputPath": "output_protected.xlsx",
           "password": "MySecretPassword"
         }'

4. 保護されたワークブックをダウンロード

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

これらのコマンドを使用すると、保護ワークフローをスクリプトや CI パイプラインに統合できます。詳細については、公式 API ドキュメントをご覧ください。

結論

JavaでRESTを使用したExcelのパスワード保護 の実装は、GroupDocs.Merger Cloud SDK for Java を使用すれば簡単です。
上記の手順に従うことで、Excelブックを保護し、サーバーサイドアプリケーションでプロセスを自動化し、機密データを安全に保つことができます。
本番環境で使用するために適切なライセンスを取得することを忘れないでください。価格情報は製品ページで確認でき、ライブラリを評価するために一時ライセンスで開始できます。
コーディングを楽しんでください!

FAQs

  • JavaでRESTを使用したExcelのパスワード保護はローカル暗号化とどう違うのですか?
    クラウド API はサーバー側で暗号化を実行するため、ローカルで暗号ライブラリを管理する必要はありません。SDK はリクエストの構築とレスポンスの解析を処理し、統合を簡素化します。

  • 追加の保護オプション(たとえば読み取り専用モード)を設定することは可能ですか?
    現在の ProtectOptions クラスはパスワード保護に焦点を当てています。より高度なセキュリティ機能については、最新のパラメータがあるかどうかAPIリファレンスをご確認ください。

  • パスワード保護に対応しているファイル形式は何ですか?
    Merger APIは現在、XLSX、DOCXPPTX、およびPDFの暗号化をサポートしています。完全なリストについては、公式ドキュメントをご参照ください。

  • 開発環境でサンプルを実行するためにライセンスは必要ですか?
    開発およびテストは一時ライセンスで実行できます。製品ページから購入できる有料ライセンスが本番環境のデプロイには必要です。

Read More