Advanced Protection Settings since Excel XP
Introduction
These protection settings restrict or allow users to:
- Delete rows or columns.
- Edit contents, objects or scenarios.
- Format cells, rows or columns.
- Insert rows, columns or hyperlinks.
- Select locked or unlocked cells.
- Use pivot tables and much more.
Aspose.Cells supports all the advanced protection settings offered by Excel XP or later versions.
Advanced Protection Settings Using Excel XP and Later Versions
To view the protection settings available in Excel XP:
- From the Tools menu, select Protection followed by Protect Sheet. A dialog will be displayed.
To view the protection settings available in Excel 2016
- From the File menu, select Protect Workbook followed by Protect Current Sheet.
- Select the Protect Sheet in the Review menu.
Following the steps mention above will show a dialog where you can allow or restrict worksheets features or apply a password to the worksheet.
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.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "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]; | |
// Restricting users to delete columns of the worksheet | |
worksheet.Protection.AllowDeletingColumn = false; | |
// Restricting users to delete row of the worksheet | |
worksheet.Protection.AllowDeletingRow = false; | |
// Restricting users to edit contents of the worksheet | |
worksheet.Protection.AllowEditingContent = false; | |
// Restricting users to edit objects of the worksheet | |
worksheet.Protection.AllowEditingObject = false; | |
// Restricting users to edit scenarios of the worksheet | |
worksheet.Protection.AllowEditingScenario = false; | |
// Restricting users to filter | |
worksheet.Protection.AllowFiltering = false; | |
// Allowing users to format cells of the worksheet | |
worksheet.Protection.AllowFormattingCell = true; | |
// Allowing users to format rows of the worksheet | |
worksheet.Protection.AllowFormattingRow = true; | |
// Allowing users to insert columns in the worksheet | |
worksheet.Protection.AllowFormattingColumn = true; | |
// Allowing users to insert hyperlinks in the worksheet | |
worksheet.Protection.AllowInsertingHyperlink = true; | |
// Allowing users to insert rows in the worksheet | |
worksheet.Protection.AllowInsertingRow = true; | |
// Allowing users to select locked cells of the worksheet | |
worksheet.Protection.AllowSelectingLockedCell = true; | |
// Allowing users to select unlocked cells of the worksheet | |
worksheet.Protection.AllowSelectingUnlockedCell = true; | |
// Allowing users to sort | |
worksheet.Protection.AllowSorting = true; | |
// Allowing users to use pivot tables in the worksheet | |
worksheet.Protection.AllowUsingPivotTable = true; | |
// Saving the modified Excel file | |
excel.Save(dataDir + "output.xls", SaveFormat.Excel97To2003); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
Cell Locking Issue
If you want to restrict users from editing cells the cells must be locked before any protection settings are applied. Otherwise, the cells can be edited even if the worksheet is protected. In Microsoft Excel XP, cells can be locked through the following dialog:
Dialog to lock cells in Excel XP |
---|
![]() |
It is possible to lock cells using the Aspose.Cells API too. Each cell can get Style formatting that contains a Boolean property, IsLocked. Set the IsLocked property to true or false to lock or unlock the cell.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
worksheet.Cells["A1"].GetStyle().IsLocked = true; | |
// Finally, Protect the sheet now. | |
worksheet.Protect(ProtectionType.All); | |
workbook.Save(dataDir + "output.xlsx"); | |