Advanced Protection Settings since Excel XP in Aspose.Cells

Advanced Protection Settings Using Excel XP and Later Versions

To view the protection settings available in Excel XP:

  1. From the Tools menu, select Protection followed by Protect Sheet.
    A dialog is displayed.

    Dialog to show protection options in Excel XP

    todo:image_alt_text

  2. Allow or restrict worksheet features or apply a password.

Advanced Protection Settings Using Aspose.Cells

Aspose.Cells supports all of the advanced protection settings.

Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a Worksheets collection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class.

The Worksheet class provides the Protection property that is used to apply these advanced protection settings. The Protection property is, in fact, an object of the Protection class that encapsulates several Boolean properties for disabling or enabling restrictions.

Below is a small example application. It opens an Excel file and uses most of the advanced protection settings supported by Excel XP and later versions.

C#

 // Creating a file stream containing the Excel file to be opened
 FileStream fstream = new FileStream("book1.xls", FileMode.Open);

 // Instantiating a Workbook object
 // Opening the Excel file through the file stream
 Workbook excel = new Workbook(fstream);

 // Accessing the first worksheet in the Excel file
 Worksheet worksheet = excel.Worksheets[0];

 // Restrict users from deleting columns in the worksheet
 worksheet.Protection.AllowDeletingColumn = false;

 // Restrict users from deleting rows in the worksheet
 worksheet.Protection.AllowDeletingRow = false;

 // Restrict users from editing contents of the worksheet
 worksheet.Protection.AllowEditingContent = false;

 // Restrict users from editing objects of the worksheet
 worksheet.Protection.AllowEditingObject = false;

 // Restrict users from editing scenarios of the worksheet
 worksheet.Protection.AllowEditingScenario = false;

 // Restrict users from filtering
 worksheet.Protection.AllowFiltering = false;

 // Allow users to format cells in the worksheet
 worksheet.Protection.AllowFormattingCell = true;

 // Allow users to format rows in the worksheet
 worksheet.Protection.AllowFormattingRow = true;

 // Allow users to format columns in the worksheet
 worksheet.Protection.AllowFormattingColumn = true;

 // Allow users to insert hyperlinks in the worksheet
 worksheet.Protection.AllowInsertingHyperlink = true;

 // Allow users to insert rows in the worksheet
 worksheet.Protection.AllowInsertingRow = true;

 // Allow users to select locked cells in the worksheet
 worksheet.Protection.AllowSelectingLockedCell = true;

 // Allow users to select unlocked cells in the worksheet
 worksheet.Protection.AllowSelectingUnlockedCell = true;

 // Allow users to sort
 worksheet.Protection.AllowSorting = true;

 // Allow users to use pivot tables in the worksheet
 worksheet.Protection.AllowUsingPivotTable = true;

 // Saving the modified Excel file
 excel.Save("output.xls", SaveFormat.Excel97To2003);

 // Closing the file stream to free all resources
 fstream.Close();

Download Running Code

Download Sample Code