The WorkbookApi interface provided by Spire.Cloud.Excel allows developer to manipulate the whole Excel document, such as creating a new Excel document and converting the Excel document to other file formats. This article demonstrates how to encrypt and decrypt Excel documents using Spire.Cloud.Excel.
Encrypt an Excel Document
import spire.cloud.excel.sdk.ApiException;
import spire.cloud.excel.sdk.Configuration;
import spire.cloud.excel.sdk.api.WorkbookApi;
import spire.cloud.excel.sdk.model.WorkbookEncryptionRequest;
public class EncryptWorkbook {
static String appId = "APP ID";
static String appKey = "APP Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on App ID, App Key and base Url
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create an instance of WorkbookApi
static WorkbookApi workbookApi = new WorkbookApi(configuration);
public static void main(String[] args) throws ApiException {
//Specify the name of the original Excel document
String name = "test.xlsx";
//Create an object of WorkbookEncryptionRequest
WorkbookEncryptionRequest encryption = new WorkbookEncryptionRequest();
//Set password for the original Excel document
encryption.password("e-iceblue");
//Specify the folder where the input document is stored, set it to null if there's none
String folder = "input";
//Store the document in the 2G default storage space offered by E-iceblue Cloud
String storage = null;
//Encrypt Excel document with the "encryptDocument" method
workbookApi.encryptDocument(name, encryption, folder, storage);
}
}
Decrypt an Excel Document
import spire.cloud.excel.sdk.ApiException;
import spire.cloud.excel.sdk.Configuration;
import spire.cloud.excel.sdk.api.WorkbookApi;
import spire.cloud.excel.sdk.model.WorkbookEncryptionRequest;
public class RemovePassword {
static String appId = "APP ID";
static String appKey = "APP Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on App ID, App Key and base Url
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create an instance of WorkbookApi
static WorkbookApi workbookApi = new WorkbookApi(configuration);
public static void main(String[] args) throws ApiException {
//Specify the name of the original Excel document
String name = "test.xlsx";
//Create an object of WorkbookEncryptionRequest
WorkbookEncryptionRequest encryption = new WorkbookEncryptionRequest();
//The password of the original Excel document
encryption.password("e-iceblue");
//Specify the folder where the input document is stored, set it to null if there's none
String folder = "input";
//Store the document in the 2G default storage space offered by E-iceblue Cloud
String storage = null;
// Remove password of the original Excel document with "decryptDocument" method
workbookApi.decryptDocument(name, encryption, folder, storage);
}
}