How to Set a Password to Workbook
Ensuring that information or data is shared with the correct individual is crucial for maintaining proper authority. With IronXL, you can create password-protected spreadsheets and secure each individual worksheet.
How to Set a Password to Workbook

- Download the C# library for password-protecting workbooks
- Access the password-protected workbook
- Apply password protection to the workbook
- Remove password protection from the workbook
- Export the encrypted workbook
Get started with IronXL
Start using IronXL in your project today with a free trial.
Access a Password-Protected Workbook
A protected spreadsheet can be opened by providing the password as the second parameter to the Load method. For example: WorkBook.Load("sample.xlsx", "IronSoftware")
.
Please note
Apply a Password to Workbook
To password-protect a spreadsheet, use the Encrypt
method as shown in the code below:
:path=/static-assets/excel/content-code-examples/how-to/set-password-workbook-protect.cs
// This code demonstrates working with the IronXL library to open, modify, and secure Excel workbooks.
// Make sure to have the IronXL library installed and properly referenced in your project.
using IronXL;
// Load a workbook from the specified file.
// Note: The file "sample.xlsx" should exist in the working directory.
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Load a password-protected workbook by providing the password "IronSoftware".
// Useful in scenarios where access to the contents of the workbook is restricted.
WorkBook protectedWorkBook = WorkBook.Load("sample.xlsx", "IronSoftware");
// Encrypt the previously loaded workbook with a specified password.
// This ensures that unauthorized users cannot open the file without the password "IronSoftware".
workBook.Encrypt("IronSoftware");
// Save any changes made to the workbook back to the original file.
// This step is crucial, especially after encrypting, to ensure protection settings are applied.
workBook.Save();
' This code demonstrates working with the IronXL library to open, modify, and secure Excel workbooks.
' Make sure to have the IronXL library installed and properly referenced in your project.
Imports IronXL
' Load a workbook from the specified file.
' Note: The file "sample.xlsx" should exist in the working directory.
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Load a password-protected workbook by providing the password "IronSoftware".
' Useful in scenarios where access to the contents of the workbook is restricted.
Private protectedWorkBook As WorkBook = WorkBook.Load("sample.xlsx", "IronSoftware")
' Encrypt the previously loaded workbook with a specified password.
' This ensures that unauthorized users cannot open the file without the password "IronSoftware".
workBook.Encrypt("IronSoftware")
' Save any changes made to the workbook back to the original file.
' This step is crucial, especially after encrypting, to ensure protection settings are applied.
workBook.Save()
Open a Password-Protected Workbook

Remove Password from Workbook
To remove the password from a spreadsheet, simply set the Password
field to null, as demonstrated in the code below:
Please note
:path=/static-assets/excel/content-code-examples/how-to/set-password-workbook-unprotect.cs
// This line of code is used to remove the password protection from a given workbook.
// In order to reset the password, the original password must be provided initially.
// The code assumes `workBook` is an instance of a valid object of a class that represents a workbook.
// The `Password` is a property of `workBook` used to manage the password protection status of the workbook.
workBook.Password = null;
// Note: Ensure that any necessary validation or unlocking
// procedures for the workbook have been handled earlier in the code pipeline for this operation to succeed.
' This line of code is used to remove the password protection from a given workbook.
' In order to reset the password, the original password must be provided initially.
' The code assumes `workBook` is an instance of a valid object of a class that represents a workbook.
' The `Password` is a property of `workBook` used to manage the password protection status of the workbook.
workBook.Password = Nothing
' Note: Ensure that any necessary validation or unlocking
' procedures for the workbook have been handled earlier in the code pipeline for this operation to succeed.
IronXL provides the ability to protect and unprotect Excel workBooks
and workSheets with a single line of C# code.
Frequently Asked Questions
How do I set a password to an Excel workbook using IronXL?
To set a password to an Excel workbook using IronXL, load the workbook with IronXL.WorkBook.Load, use the Encrypt method with your desired password, and save the changes.
How can I access a password-protected workbook using IronXL?
You can access a password-protected workbook by providing the password as the second parameter to the WorkBook.Load method.
What is the method to remove a password from an Excel workbook in IronXL?
To remove a password from an Excel workbook, load the protected workbook with the original password, then set the Password field to null using the Decrypt method, and save the workbook.
Is it possible to open a password-protected spreadsheet without the correct password using IronXL?
No, it is not possible to open a password-protected spreadsheet without the correct password using IronXL.
Where can I download the IronXL library for password-protecting workbooks?
You can download the IronXL library for password-protecting workbooks from the NuGet website at https://nuget.org/packages/IronXL.Excel/.
Can IronXL be used to protect individual worksheets in addition to workbooks?
Yes, IronXL can be used to protect and unprotect both Excel workbooks and worksheets using C# code.