DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 93 posts at DZone. View Full User Profile

Protect Excel Workbooks Using .NET, Java, PHP, Python & More in Cloud

04.30.2014
| 13098 views |
  • submit to reddit
//Please take a look over the following code snippet for Protecting Excel Workbooks

//[C#]
 
//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
    }

//[PHP Code]
 
AsposeApp::$appSID = "77******-1***-4***-a***-80**********";
AsposeApp::$appKey = "********************************";

AsposeApp::$outPutLocation = getcwd() . "/Output/";

//build URI to protect workbook
$strURI = 'http://api.aspose.com/v1.1/cells/Sample.xlsx/protection';

//Build JSON to post
$fieldsArray["ProtectionType"] = "all";
$fieldsArray["Password"] = "abc";
$json = json_encode($fieldsArray);

$signedURI = Utils::sign($strURI);

$responseStream = Utils::processCommand($signedURI, "POST", "json", $json);

//Download output file
$strURI = "http://api.aspose.com/v1.1/storage/file/Sample.xlsx";
$signedURI = Utils::sign($strURI);

$responseStream = Utils::processCommand($signedURI, "GET", "", "");
Utils::saveFile($responseStream, AsposeApp::$outPutLocation . "Sample.xlsx");

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