IronXL How-Tos Set Password to Worksheet How to Set Password to Worksheet ByChaknith Bin March 14, 2023 Updated June 22, 2025 Share: 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. View the IronXL YouTube Playlist 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. First Step: Start for Free 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() $vbLabelText $csharpLabel 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() $vbLabelText $csharpLabel 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 in C#? To password protect an Excel worksheet, use IronXL's `ProtectSheet` method with a password as a parameter. For example, `workSheet.ProtectSheet("IronXL")`. How do I access a password-protected worksheet in C#? 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 in C#? To remove a password from a specific worksheet using IronXL, use the `UnprotectSheet` method. Call `workSheet.UnprotectSheet()` to remove any password associated with the worksheet. What is the first step to password protect worksheets in C#? The first step is to download the IronXL C# library to enable password protection for worksheets. You can get it from NuGet. Can a protected worksheet be modified in C#? 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 C#? After applying protection using IronXL, 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 in C#? Yes, IronXL allows you to export the spreadsheet to different formats after applying or removing password protection. How is read-only authentication for worksheets handled in C#? 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 worksheet protection in C#? To begin using IronXL for worksheet protection, you need to download the library and reference it in your C# project. Chaknith Bin Chat with engineering team now Software Engineer Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking. Ready to Get Started? Free NuGet Download Total downloads: 1,446,926 View Licenses