How to Set a Password on an Excel Worksheet in C#

In this tutorial, we explore the process of password protecting Excel worksheets using the IronXL library in C#. The video begins by guiding viewers to install the IronXL namespace via the Noug Get package manager. Once installed, the tutorial demonstrates how to load an existing workbook from the C drive using the workbook load function. The focus then shifts to applying protection on a selected worksheet with the 'protect sheet' method, using 'iron XL' as the password. After setting the protection, the workbook is saved to apply changes. An interesting feature highlighted in the tutorial is the ability to unprotect the worksheet programmatically without entering the password by uncommenting a specific line of code. The video concludes with a demonstration of the project in action, where the Excel file is opened, and the protection is validated by entering the password. This tutorial provides a comprehensive guide for developers looking to secure their Excel data programmatically, offering a practical demonstration of IronXL's capabilities.

// Make sure to install the IronXL package via the Package Manager Console with:
// Install-Package IronXL.Excel

using IronXL;

namespace ExcelProtectionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load the workbook from the C drive
            WorkBook workbook = WorkBook.Load("C:\\path\\to\\your\\workbook.xlsx");

            // Select the worksheet you want to protect
            WorkSheet sheet = workbook.WorkSheets.First();

            // Protect the worksheet with a password
            sheet.ProtectSheet("iron XL");

            // Save the workbook with applied changes
            workbook.SaveAs("C:\\path\\to\\your\\protected_workbook.xlsx");

            // Uncomment the line below to unprotect the worksheet programmatically
            // sheet.UnprotectSheet("iron XL");
        }
    }
}
// Make sure to install the IronXL package via the Package Manager Console with:
// Install-Package IronXL.Excel

using IronXL;

namespace ExcelProtectionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load the workbook from the C drive
            WorkBook workbook = WorkBook.Load("C:\\path\\to\\your\\workbook.xlsx");

            // Select the worksheet you want to protect
            WorkSheet sheet = workbook.WorkSheets.First();

            // Protect the worksheet with a password
            sheet.ProtectSheet("iron XL");

            // Save the workbook with applied changes
            workbook.SaveAs("C:\\path\\to\\your\\protected_workbook.xlsx");

            // Uncomment the line below to unprotect the worksheet programmatically
            // sheet.UnprotectSheet("iron XL");
        }
    }
}
' Make sure to install the IronXL package via the Package Manager Console with:
' Install-Package IronXL.Excel

Imports IronXL

Namespace ExcelProtectionExample
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Load the workbook from the C drive
			Dim workbook As WorkBook = WorkBook.Load("C:\path\to\your\workbook.xlsx")

			' Select the worksheet you want to protect
			Dim sheet As WorkSheet = workbook.WorkSheets.First()

			' Protect the worksheet with a password
			sheet.ProtectSheet("iron XL")

			' Save the workbook with applied changes
			workbook.SaveAs("C:\path\to\your\protected_workbook.xlsx")

			' Uncomment the line below to unprotect the worksheet programmatically
			' sheet.UnprotectSheet("iron XL");
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

Further Reading: How to Set Password to Worksheet

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Sort Cell Ranges in Excel Using IronXL
NEXT >
How to Set a Password for a Workbook in C#