Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Further Reading: How to Set Password to Worksheet