Concealing sensitive data in Excel workbooks is a common requirement for enterprises that share reports internally or with partners. GroupDocs.Merger Cloud SDK for Java enables developers to add password protection to XLSX files through a simple REST API. In this guide you will learn how to implement password Protect Excel via REST in Java, see a complete Java example, and run equivalent cURL commands for cloud integration.
Prerequisites and Setup
Before you begin, make sure you have the following:
- Java 8 or higher installed.
- Maven or Gradle for dependency management.
- A GroupDocs Cloud account with client ID and client secret.
- Access to the cloud storage where the Excel files will reside.
Add the Maven dependency to your pom.xml (or the equivalent Gradle snippet):
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-merger-cloud</artifactId>
<version>25.11</version>
</dependency>
You can download the latest JARs from the download page. After adding the dependency, create a configuration object with your credentials:
String clientId = "YOUR_CLIENT_ID";
String clientSecret = "YOUR_CLIENT_SECRET";
Configuration config = new Configuration(clientId, clientSecret);
MergerApi mergerApi = new MergerApi(config);
With the client ready, you can move on to the actual password‑protect workflow.
Password Protect Excel via REST in Java: Step-by-Step Walkthrough
Step 1: Load the Source Document
Define the location of the source XLSX file inside your cloud storage.
FileInfo inputFile = new FileInfo();
inputFile.setFilePath("input.xlsx"); // source Excel file
Step 2: Set the Destination Path
Specify where the protected workbook will be saved.
String outputPath = "output_protected.xlsx"; // destination file
Step 3: Configure Password Protection Options
Create a ProtectOptions object, attach the file info, output path, and the desired password.
ProtectOptions protectOptions = new ProtectOptions();
protectOptions.setFileInfo(inputFile);
protectOptions.setOutputPath(outputPath);
protectOptions.setPassword("MySecretPassword"); // desired password
Step 4: Build the Protect Document Request
Wrap the options in a ProtectDocumentRequest.
ProtectDocumentRequest request = new ProtectDocumentRequest(protectOptions);
Step 5: Execute the Request and Handle the Response
Call the API method and process any errors.
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());
}
For more details on each class, refer to the API reference.
Full Working Example for Password Protect Excel via REST in Java
The following code demonstrates the complete process from start to finish.
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());
}
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (
input.xlsx,output_protected.xlsx) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Protect Excel Workbook via REST API Using cURL
Below is a set of cURL commands that perform the same password‑protect operation directly against the REST endpoints.
1. Authenticate and obtain an access token
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"
}'
The response contains an access_token that you will use in subsequent calls.
2. Upload the source XLSX file
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. Request password protection
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. Download the protected workbook
curl -X GET "https://api.groupdocs.cloud/v2.0/storage/file/output_protected.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o output_protected.xlsx
These commands let you integrate the protection workflow into scripts or CI pipelines. For more details, see the official API documentation.
Conclusion
Implementing password Protect Excel via REST in Java is straightforward with the GroupDocs.Merger Cloud SDK for Java. By following the steps above you can secure Excel workbooks, automate the process in server‑side applications, and keep sensitive data safe. Remember to acquire a proper license for production use; pricing information is available on the product page and you can start with a temporary license to evaluate the library. Happy coding!
FAQs
How does password Protect Excel via REST in Java differ from local encryption?
The cloud API performs encryption on the server, so you don’t need to manage cryptographic libraries locally. The SDK handles request construction and response parsing, simplifying integration.Is it possible to set additional protection options, such as read‑only mode?
The currentProtectOptionsclass focuses on password protection. For more advanced security features, consult the API reference for any newer parameters.What file formats are supported for password protection?
The Merger API currently supports XLSX, DOCX, PPTX, and PDF for encryption. Refer to the official documentation for the full list.Do I need a license to run the example in a development environment?
Development and testing can be done with a temporary license. Production deployments require a paid license, which you can purchase through the product page.
