Spire.Cloud.Excel provides the WorkbookApi interface to work with Excel workbooks. This article shows you how to encrypt or decrypt Excel workbooks by using this API.
Example 1. Encrypt Excel Document
using System;
using Spire.Cloud.Excel.Sdk.Api;
using Spire.Cloud.Excel.Sdk.Client;
using Spire.Cloud.Excel.Sdk.Model;
namespace EncryptExcel
{
class Program
{
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 a WorkbookApi object
static WorkbookApi workbookApi = new WorkbookApi(configuration);
static void Main(string[] args)
{
//Specify the source document name
string name = "sample.xlsx";
//Create a WorkbookEncryptionRequest object
WorkbookEncryptionRequest encryption = new WorkbookEncryptionRequest();
//Specify password
encryption.Password = "abc-123";
//Specify the folder where the source document is stored
string folder = "input";
//Use the default storage, set to null
string storage = null;
//Call EncryptDocument method to encrypt document with password
workbookApi.EncryptDocument(name, encryption, folder, storage);
}
}
}
Example 2. Decrypt Excel Document
using System;
using Spire.Cloud.Excel.Sdk.Api;
using Spire.Cloud.Excel.Sdk.Client;
using Spire.Cloud.Excel.Sdk.Model;
namespace DecryptExcel
{
class Program
{
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 a WorkbookApi object
static WorkbookApi workbookApi = new WorkbookApi(configuration);
static void Main(string[] args)
{
//Specify the source document name
string name = "sample.xlsx";
//Create a WorkbookEncryptionRequest object
WorkbookEncryptionRequest encryption = new WorkbookEncryptionRequest();
//Specify the original password
encryption.Password = "abc-123";
//Specify the folder where the source document is stored
string folder = "input";
//Use the default storage, set to null
string storage = null;
//Call DecryptDocument method to remove password from the encrypted document
workbookApi.DecryptDocument(name, encryption, folder, storage);
}
}
}