WorksheetProtect Method (ProtectionType, String, String)
Protects worksheet.

Namespace: Aspose.Cells
Assembly: Aspose.Cells (in Aspose.Cells.dll) Version: 19.11.0.0 (19.11)
Syntax
public void Protect(
	ProtectionType type,
	string password,
	string oldPassword
)

Parameters

type
Type: Aspose.CellsProtectionType
Protection type.
password
Type: SystemString
Password.
oldPassword
Type: SystemString
If the worksheet is already protected by a password, please supply the old password. Otherwise, you can set a null value or blank string to this parameter.
Remarks
This method can protect worksheet in all versions of Excel file.
Examples
[C#]



//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("C:\\book1.xls", FileMode.Open);
//Instantiating a Workbook object and 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];
//Protecting the worksheet with a password
worksheet.Protect(ProtectionType.All, "aspose", null);
//Saving the modified Excel file in default (that is Excel 20003) format
excel.Save("C:\\output.xls");
//Closing the file stream to free all resources
fstream.Close();

[Visual Basic]

'Creating a file stream containing the Excel file to be opened
Dim fstream As FileStream = New FileStream("C:\\book1.xls", FileMode.Open)
'Instantiating a Workbook object and Opening the Excel file through the file stream
Dim excel As Workbook = New Workbook(fstream)
'Accessing the first worksheet in the Excel file
Dim worksheet As Worksheet = excel.Worksheets(0)
'Protecting the worksheet with a password
worksheet.Protect(ProtectionType.All, "aspose", DBNull.Value.ToString())
'Saving the modified Excel file in default (that is Excel 20003) format
excel.Save("C:\\output.xls")
'Closing the file stream to free all resources
fstream.Close()
See Also