How to Set Password to Workbook

Giving information or data to the correct individual is very important in conveying a correct authority. IronXL makes it possible to create password protected spreadsheet as well as each individual WorkSheet.

C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
C# Excel DLL

Download DLL

Download DLL

Manually install into your project

Accessing Password Protected Workbook

Protected spreadsheet can be opened by providing password as the second parameter to Load method. For example, WorkBook.Load("sample.xlsx", "IronSoftware").

Please note: It is not possible to open protected spreadsheet without the correct password

Applying Password to Workbook

Use Encrypt method to password protect any spreadsheet. For example, workBook.Encrypt("IronSoftware").

:path=/static-assets/excel/content-code-examples/how-to/set-password-workbook-protect.cs
using IronXL;

WorkBook workBook = WorkBook.Load("sample.xlsx");

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

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

workBook.Save();
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

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

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

workBook.Save()
VB   C#

Open Password Protected Workbook

Open Protected Spreadsheet

Removing Password from Workbook

Removing password from spreadsheet is as simple as setting null to Password field as demonstrated workBook.Password = null.

Please note: This action can only be done after accessing the workbook. Hence, knowing the original password is a must.

:path=/static-assets/excel/content-code-examples/how-to/set-password-workbook-unprotect.cs
// Remove protection for opened workbook. Original password is required.
workBook.Password = null;
' Remove protection for opened workbook. Original password is required.
workBook.Password = Nothing
VB   C#

IronXL allows you to Protect and Unprotect any Excel WorkBook and WorkSheet with a single line of C# code.