在 Excel 工作簿中隱藏敏感資料是企業在內部或與合作夥伴共享報告時的常見需求。 GroupDocs.Merger Cloud SDK for Java 使開發人員能夠通過簡單的 REST API 為 XLSX 檔案添加密碼保護。在本指南中,您將學習如何 在 Java 中通過 REST 為 Excel 加密保護,查看完整的 Java 示例,並運行等效的 cURL 命令以進行雲端整合。
先決條件與設定
在開始之前,請確保您具備以下項目:
- 已安裝 Java 8 或更高版本。
- 用於依賴管理的 Maven 或 Gradle。
- 具備 client ID 和 client 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);
當客戶端已就緒後,您可以繼續實際的密碼保護工作流程。
使用 REST 在 Java 中對 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:建立保護文件請求
將選項包裝在 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 參考。
完整的工作範例:使用 REST 在 Java 中對 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.xlsx、output_protected.xlsx)以匹配實際文件位置,驗證所有必需的依賴項已正確安裝,並在開發環境中徹底測試。如遇任何問題,請參閱官方文檔或聯繫支援團隊尋求協助。
使用 cURL 透過 REST API 保護 Excel 工作簿
以下是一組 cURL 命令,直接對 REST 端點執行相同的密碼保護操作。
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 文件。
結論
使用 GroupDocs.Merger Cloud SDK for Java,在 Java 中通過 REST 實現 Excel 密碼保護 非常簡單。按照上述步驟操作,您可以保護 Excel 工作簿的安全,將此過程自動化於伺服器端應用程式,並確保敏感資料安全。請記得為生產環境獲取合適的授權;產品頁面上提供了價格資訊,您也可以使用 temporary license 來評估此函式庫。祝開發愉快!
常見問題
使用 Java 透過 REST 進行 Excel 密碼保護與本地加密有何不同?
雲端 API 在伺服器上執行加密,因此您無需在本地管理加密函式庫。SDK 會處理請求的構建與回應的解析,簡化了整合流程。是否可以設定其他保護選項,例如唯讀模式?
目前的ProtectOptions類別專注於密碼保護。若需更高階的安全功能,請參閱 API 參考 以了解任何更新的參數。支援哪些檔案格式的密碼保護?
Merger API 目前支援 XLSX、DOCX、PPTX,以及 PDF 進行加密。請參閱官方文件以獲取完整列表。我需要許可證才能在開發環境中運行示例嗎?
開發和測試可以使用臨時許可證完成。生產部署需要付費許可證,您可以通過產品頁面購買。
