Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer

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
It is not possible to open a protected spreadsheet without the correct password

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()
$vbLabelText   $csharpLabel

Open a Password-Protected Workbook

Open Protected Spreadsheet

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
This action can only be performed after accessing the workbook. Therefore, it is necessary to know the original password.

: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.
$vbLabelText   $csharpLabel

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.

Chaknith related to Remove Password from Workbook
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.