Home > Code > C# > Protect Excel Workbooks (Data, Content & Formatting) Using Cloud APIs

Protect Excel Workbooks (Data, Content & Formatting) Using Cloud APIs

by sher azam   on Apr 30, 2014   Category: C#   |  Views: 1471    |  Points: 25   |  Gold 


This technical tip shows how developers can protect MS Excel workbooks using Aspose.Cells for Cloud API inside their own cloud applications. When a worksheet is protected, user's actions are restricted. For example, you cannot input data. Moreover, you can't insert or delete rows or columns etc. There are mostly 3 protection options in MS Excel, such as protecting Contents, Objects or Scenarios. Protected worksheet doesn't hide or protect sensitive data, so it's different from file encryption. Generally, worksheet protection is suitable for presentation purposes. It prevents the end users from modifying the data, content and formatting in the worksheet. Developers can use Aspose REST API with any language for their choice such as .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more. For code samples and other details please visit the main page.

Please take a look over the following code snippet for Protecting Excel Workbooks

//[C# Code]

 
//build URI
string strURI = "http://api.aspose.com/v1.1/cells/input.xls/protection"

string signedURI = Sign(strURI);

//serialize the JSON request content
Protection protection = new Protection();
protection.ProtectionType = protectionType;
protection.Password = password;

string strJSON = JsonConvert.SerializeObject(protection);

Stream responseStream = ProcessCommand(signedURI, "POST", strJSON);

StreamReader reader = new StreamReader(responseStream);
string strResponse = reader.ReadToEnd();

//Parse the json string to JObject
JObject pJSON = JObject.Parse(strResponse);

//Here is the Protection Class used above
public class Protection
{
public Protection()
{

}

public ProtectionType ProtectionType { get; set;}
public string Password { get; set;}

}

/// <summary>
/// Represents Protection Types
/// </summary>
public enum ProtectionType
{
All,
Structure,
Windows,
None
}

//[Java Code]

AsposeApp.setAppSID("77******-1***-4***-a***-8***********");
AsposeApp.setAppKey("89******************************");

//build URI to Protect Workbook
string strURI = "http://api.aspose.com/v1.1/cells/input.xls/protection";

//sign URI
string signedURI = Sign(strURI);

//serialize the JSON request content
Protection protection = new Protection();
protection.setProtectionType(ProtectionType.All);
protection.setPassword("password");

String strJSON = "";

Gson gson = new Gson();

strJSON = gson.toJson(encryption, Encryption.class);

InputStream responseStream = ProcessCommand(signedURI, "POST", strJSON);

String strResponse = StreamToString(responseStream);

//Following is the Protection Class used above
public class Protection
{
public Protection()
{

}

private ProtectionType ProtectionType;
private String Password;

public ProtectionType getProtectionType(){return ProtectionType;}
public String getPassword(){return Password;}

public void setProtectionType(ProtectionType ProtectionType){ this.ProtectionType=ProtectionType;}
public void setPassword(String Password ){ this.Password=Password;}


}

/// <summary>
/// Represents Protection Types
/// </summary>
public enum ProtectionType
{
All,
Structure,
Windows,
None
}



More about Aspose.Cells for Cloud

- Homepage of Aspose.Cells for Cloud: http://www.aspose.com/cloud/excel-api.aspx

- Read More Technical Tips by Aspose.Cells for Cloud: http://www.aspose.com/docs/display/cellscloud/Working+with+Workbook



Post Code  |  Code Snippet Home

User Responses


  Re :Protect Excel Workbooks (Data, Content & Formatting) Using Cloud APIs   
Posted by Leo_21
on 24/07/2014
Points : 5

hi,

I use below method to protect Excel workbook ,this solution is based on Spire.XLS component (http://spreadsheet.codeplex.com/).check

     //Load Workbook
Workbook book = new Workbook();
book.LoadFromFile(@"E:\Work\Documents\VendorInfo.xlsx");
//Protect Workbook
book.Protect("abc-123");
//Save and Launch
book.SaveToFile("ProtectExcel.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("ProtectExcel.xlsx");


Hope this can be option for proctect Excel sheet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts