Xceed Workbooks for .NET v1.4 Documentation
Protect Method (Worksheet)
Example 


Xceed.Workbooks.NET Assembly > Xceed.Workbooks.NET Namespace > Worksheet Class : Protect Method

The WorksheetProtection object that lets you select which actions can be carried out on a protected Worksheet.

When set to null, the default WorksheetProtection object will be used, which will allow only two actions to be carried out : selecting locked and unlocked Cells.

The password that will be used to protect the Worksheet.

Note that the same password must be used to Unprotect the Worksheet. However, you can protect a Worksheet without using a password. The default setting is, in fact, null, which sets no password for the Protection.

Protects a Worksheet by enabling or disabling specific actions.

This can be done with or without a password.

Syntax
'Declaration

 

Public Sub Protect( _

   Optional ByVal protection As WorksheetProtection, _

   Optional ByVal password As String _

) 
'Usage

 

Dim instance As Worksheet

Dim protection As WorksheetProtection

Dim password As String

 

instance.Protect(protection, password)
public void Protect( 

   WorksheetProtection protection,

   string password

)

Parameters

protection

The WorksheetProtection object that lets you select which actions can be carried out on a protected Worksheet.

When set to null, the default WorksheetProtection object will be used, which will allow only two actions to be carried out : selecting locked and unlocked Cells.

password

The password that will be used to protect the Worksheet.

Note that the same password must be used to Unprotect the Worksheet. However, you can protect a Worksheet without using a password. The default setting is, in fact, null, which sets no password for the Protection.

Remarks
You cannot protect a Worksheet which is already protected; you must Unprotect it first.
Example
using( var workbook = Workbook.Create( "test.xlsx" ) )



 {

   // Gets the first worksheet. A workbook contains at least 1 worksheet.

   var worksheet = workbook.Worksheets[ 0 ];



   // Sets content.

   worksheet.Cells[ "C6" ].Value = "This worksheet is protected with a password. Only formatting cell and inserting rows/columns is allowed.";

   worksheet.Cells[ "C8" ].Value = "The worksheet can be unprotected through the 'Review-Unprotect sheet' option by typing 'xceed'.";



   // Sets the worksheet protection : only formatting cells and inserting rows/columns will be allowed.

   var protection = new WorksheetProtection() { AllowFormatCells = true, AllowInsertRows = true, AllowInsertColumns = true };

   // Protects the 1st worksheet with a password.

   worksheet.Protect( protection, "xceed" );



   // Adds a 2nd worksheet.

   var worksheet2 = workbook.Worksheets.Add();



   // Sets content in 2nd worksheet.

   worksheet2.Cells[ "B5" ].Value = "This worksheet is NOT protected.";



   workbook.Save();

 }
Requirements

Target Platforms: Windows 11, Windows 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

Worksheet Class
Worksheet Members