This article demonstrates how to protect or unprotect Excel worksheet using the WorksheetApi interface provided by Spire.Cloud.Excel.
Protect Excel worksheet
using Spire.Cloud.Excel.Sdk.Api;
using Spire.Cloud.Excel.Sdk.Client;
using Spire.Cloud.Excel.Sdk.Model;
using System;
namespace ProtectWorksheet
{
class Program
{
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration instance based on App ID, App Key and base URL
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create a WorksheetApi instance
static WorksheetsApi worksheetsApi = new WorksheetsApi(configuration);
static void Main(string[] args)
{
//Specify the input document
string name = "sample.xlsx";
//Specify the folder storing the input document, and it's "null" if nothing
string folder = "input";
//Use the 2G storage provided by E-iceblue, and it's "null" by default
string storage = null;
//Specify the name of the worksheet that you want to protect
string sheetName = "Sheet1";
//Create a ProtectSheetParameter instance
ProtectSheetParameter protectParameter = new ProtectSheetParameter();
//Set password to protect the worksheet
protectParameter.Password = "123";
//Set protection type
protectParameter.ProtectionType = "scenarios";
//Allow selecting locked cell
protectParameter.AllowSelectingLockedCell = "true";
//Allow deleting column
protectParameter.AllowDeletingColumn = "true";
//Allow formatting row
protectParameter.AllowFormattingRow = "true";
//Call ProtectWorksheet method to protect the worksheet
worksheetsApi.ProtectWorksheet(name, sheetName, protectParameter, folder, storage);
}
}
}
Output:
Unprotect Excel worksheet
using Spire.Cloud.Excel.Sdk.Api;
using Spire.Cloud.Excel.Sdk.Client;
using Spire.Cloud.Excel.Sdk.Model;
using System;
namespace UnprotectWorksheet
{
class Program
{
static String appId = "APP ID";
static String appKey = "APP Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration instance based on App ID, App Key and base URL
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create a WorksheetApi instance
static WorksheetsApi worksheetsApi = new WorksheetsApi(configuration);
static void Main(string[] args)
{
//Specify the input document
string name = "sample.xlsx";
//Specify the folder storing the input document, and it's "null" if nothing
string folder = "input";
//Use the 2G storage provided by E-iceblue, and it's "null" by default
string storage = null;
//Specify the name of the worksheet that you want to unprotect
string sheetName = "Sheet1";
//Create a ProtectSheetParameter instance
ProtectSheetParameter protectParameter = new ProtectSheetParameter();
//Set password to unprotect the worksheet
protectParameter.Password = "123";
//Call UnProtectWorksheet method to unprotect the worksheet
worksheetsApi.UnProtectWorksheet(name, sheetName, protectParameter, folder, storage);
}
}
}