How to Set Password to Worksheet
Restricting a worksheet with a Read-Only authentication is a very common requirement for data files. IronXL makes it easy to apply Read-Only protection to worksheets in .NET.
How to Password Protect Worksheets

- Download the C# library to password protect worksheets
- Access the password-protected worksheet in the opened workbook
- Apply password protection to the selected worksheet
- Remove password protection from the selected worksheet
- Export the spreadsheet to different spreadsheet formats
Get started with IronXL
Start using IronXL in your project today with a free trial.
Access a Password Protected Worksheet
IronXL allows you to access and modify any protected worksheet without requiring the password. Once the spreadsheet is opened with IronXL, you can modify any cell in any worksheet.
Apply Password to Worksheet
To restrict modifications to a worksheet while allowing users to view its content in Excel, use the ProtectSheet
method with a password as a parameter. For example, workSheet.ProtectSheet("IronXL")
. This sets a password-based ReadOnly authentication for the selected worksheet.
:path=/static-assets/excel/content-code-examples/how-to/set-password-worksheet-protect.cs
using IronXL;
// Load the Excel workbook
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Get the default worksheet from the workbook
// The default worksheet is typically the first one in the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Protect the default worksheet with a password
// This ensures that the worksheet cannot be modified without the password.
// Note: Use a strong password to ensure your worksheet is secure.
workSheet.ProtectSheet("IronXL");
// Save the changes to the workbook
// This writes the protection to the file system, making it persistent
workBook.Save();
Imports IronXL
' Load the Excel workbook
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Get the default worksheet from the workbook
' The default worksheet is typically the first one in the workbook
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Protect the default worksheet with a password
' This ensures that the worksheet cannot be modified without the password.
' Note: Use a strong password to ensure your worksheet is secure.
workSheet.ProtectSheet("IronXL")
' Save the changes to the workbook
' This writes the protection to the file system, making it persistent
workBook.Save()
Open a Password-Protected Worksheet

Remove Password from Worksheet
To remove a password from a specific worksheet, you can use the UnprotectSheet
method. Simply call workSheet.UnprotectSheet()
to remove any password associated with the worksheet.
:path=/static-assets/excel/content-code-examples/how-to/set-password-worksheet-unprotect.cs
// The following line of code unprotects the worksheet without requiring a password.
//
// Note: This assumes that 'workSheet' is an instance of a worksheet object from a spreadsheet library
// such as EPPlus or another library that uses similar method calls.
//
// If the library you're using provides the Unprotect() method without parameters,
// this will remove protection from 'workSheet' without needing a password,
// enabling you to modify the contents of the worksheet.
workSheet.Unprotect();
' The following line of code unprotects the worksheet without requiring a password.
'
' Note: This assumes that 'workSheet' is an instance of a worksheet object from a spreadsheet library
' such as EPPlus or another library that uses similar method calls.
'
' If the library you're using provides the Unprotect() method without parameters,
' this will remove protection from 'workSheet' without needing a password,
' enabling you to modify the contents of the worksheet.
workSheet.Unprotect()
IronXL allows you to protect and unprotect any Excel workbook and worksheet with a single line of C# code.
Frequently Asked Questions
How can I password protect an Excel worksheet using IronXL?
To password protect an Excel worksheet using IronXL, use the `ProtectSheet` method with a password as a parameter. For example, `workSheet.ProtectSheet("IronXL")`.
How do I access a password-protected worksheet with IronXL?
IronXL allows you to access and modify any protected worksheet without requiring the password. Once the spreadsheet is opened with IronXL, you can modify any cell in any worksheet.
How can I remove a password from a worksheet using IronXL?
To remove a password from a specific worksheet, use the `UnprotectSheet` method. Call `workSheet.UnprotectSheet()` to remove any password associated with the worksheet.
What is the first step to password protect worksheets using IronXL?
The first step is to download the IronXL C# library to enable password protection for worksheets. You can get it from NuGet.
Can IronXL modify a protected worksheet?
Yes, IronXL allows you to access and modify any protected worksheet without needing the password, once the spreadsheet is opened.
What method is used to save changes after protecting a worksheet in IronXL?
After applying protection, use the `SaveAs` method to save the changes to the workbook, for example, `workbook.SaveAs("protected_example.xlsx")`.
Is it possible to export the spreadsheet to different formats using IronXL?
Yes, IronXL allows you to export the spreadsheet to different formats after applying or removing password protection.
How does IronXL handle read-only authentication for worksheets?
IronXL makes it easy to apply read-only protection to worksheets by using the `ProtectSheet` method with a password, allowing users to view content but restrict modifications.
What is required to begin using IronXL for worksheet protection?
To begin using IronXL for worksheet protection, you need to download the library and reference it in your C# project.