IRONSOFTWAREHOME
Protect Excel Files
using IronXL;

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

// Open protected spreadsheet file
WorkBook protectedWorkBook = WorkBook.Load("sample.xlsx", "IronSoftware");

// Spreadsheet protection
// Set protection for spreadsheet file
workBook.Encrypt("IronSoftware");

// Remove protection for spreadsheet file. Original password is required.
workBook.Password = null;

workBook.Save();

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

// Remove protection for particular worksheet. It works without password!
workSheet.UnprotectSheet();

workBook.Save();
Imports IronXL

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

' Open protected spreadsheet file
Dim protectedWorkBook As WorkBook = WorkBook.Load("sample.xlsx", "IronSoftware")

' Spreadsheet protection
' Set protection for spreadsheet file
workBook.Encrypt("IronSoftware")

' Remove protection for spreadsheet file. Original password is required.
workBook.Password = Nothing

workBook.Save()

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

' Remove protection for particular worksheet. It works without password!
workSheet.UnprotectSheet()

workBook.Save()
NuGet Download
PM > Install-Package IronXL.Excel
Protect Excel Files

Protect Excel Files

The Encrypt method of IronXL provides developers with the ability to apply password protection for both entire Excel workbooks and individual worksheets. This capability is crucial for safeguarding sensitive data and ensuring that only authorized users can access or modify the contents of an Excel file.

IronXL also allows for sheet-level protection, which can prevent users from editing certain parts of the workbook or specific worksheets. This is particularly useful in collaborative environments where you want to restrict editing rights but still allow users to view or interact with data. Additionally, the option to remove protection with or without a password offers flexibility, making it suitable for both secure environments and situations where access needs to be temporarily restored. Overall, this feature is an essential tool for ensuring Excel file security in automated data processing workflows.

In this code, a workbook is first loaded from the file sample.xlsx using IronXL's WorkBook.Load() method. After the workbook is loaded, the Encrypt() method is used to apply password protection to the entire workbook, with the password set as myP@ssw0rd. This ensures that the workbook cannot be opened or modified without the correct password.

Next, the default worksheet of the workbook is accessed using the DefaultWorkSheet property. The ProtectSheet() method is then called on this worksheet, applying a password ("myP@ssw0rd_sheet") that restricts editing on the sheet level. This prevents users from making changes to the protected sheet unless they provide the correct password.

Finally, the Save() method is invoked to save the changes made to both the workbook and the worksheet, ensuring that the protection settings are preserved in the saved file. This ensures that the workbook and its worksheet are both securely protected and can only be accessed or modified with the respective passwords.

Learn How to Set Passwords on Excel Workbooks in C#
View on GitHub

Ready to Get Started?

Nuget Downloads 2,237,574Version:2026.9just released

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required