How to Set Password to Worksheet

by Chaknith Bin

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.


C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
C# Excel DLL

Download DLL

Download DLL

Manually install into your project

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;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set protection for selected worksheet
workSheet.ProtectSheet("IronXL");

workBook.Save();
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set protection for selected worksheet
workSheet.ProtectSheet("IronXL")

workBook.Save()
VB   C#

Open a Password-Protected Worksheet

Access 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
// Remove protection for selected worksheet. It works without password!
workSheet.UnprotectSheet();
' Remove protection for selected worksheet. It works without password!
workSheet.UnprotectSheet()
VB   C#

IronXL allows you to protect and unprotect any Excel workbook and worksheet with a single line of C# code.

Chaknith Bin

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.