How to Set a Password for a Workbook in C#

In the video tutorial, viewers are guided through the process of setting a password for an Excel workbook using IronXL in C#. The tutorial begins with ensuring that IronXL is installed and then proceeds to load an existing Excel file, 'sample.xlsx', into the workbook. It demonstrates how to handle both non-protected and protected files by passing a password as a second argument when necessary. The main focus is on applying a security measure to the spreadsheet by utilizing the encrypt method to lock the workbook with a password. In this case, the password 'iron software' is used. After applying the password, the file is saved, and the project is executed, requiring the password to unlock the workbook. The successful application of password protection is confirmed by entering the password and accessing the file contents. The tutorial concludes by encouraging viewers to explore more functionalities of the IronXL library and inviting them to subscribe for additional tips and tricks. A link is provided in the description for downloading and installing IronXL to experience its capabilities firsthand.

C# Code Example: Password Protecting an Excel Workbook

Below is an example of how you can use IronXL in C# to password-protect an Excel workbook:

// Make sure to add IronXL to your project by installing the IronXL NuGet package

using IronXL;

class Program
{
    static void Main()
    {
        // Load an existing Excel file
        WorkBook workbook = WorkBook.Load("sample.xlsx");

        // Set a password for the workbook using the Encrypt method
        // The password used here is 'iron software'
        workbook.Encrypt("iron software");

        // Save the workbook as a new file, now password protected
        workbook.SaveAs("protected_sample.xlsx");

        // Inform the user that the workbook is now password protected
        Console.WriteLine("The workbook has been protected with a password and saved as 'protected_sample.xlsx'.");
    }
}
// Make sure to add IronXL to your project by installing the IronXL NuGet package

using IronXL;

class Program
{
    static void Main()
    {
        // Load an existing Excel file
        WorkBook workbook = WorkBook.Load("sample.xlsx");

        // Set a password for the workbook using the Encrypt method
        // The password used here is 'iron software'
        workbook.Encrypt("iron software");

        // Save the workbook as a new file, now password protected
        workbook.SaveAs("protected_sample.xlsx");

        // Inform the user that the workbook is now password protected
        Console.WriteLine("The workbook has been protected with a password and saved as 'protected_sample.xlsx'.");
    }
}
' Make sure to add IronXL to your project by installing the IronXL NuGet package

Imports IronXL

Friend Class Program
	Shared Sub Main()
		' Load an existing Excel file
		Dim workbook As WorkBook = WorkBook.Load("sample.xlsx")

		' Set a password for the workbook using the Encrypt method
		' The password used here is 'iron software'
		workbook.Encrypt("iron software")

		' Save the workbook as a new file, now password protected
		workbook.SaveAs("protected_sample.xlsx")

		' Inform the user that the workbook is now password protected
		Console.WriteLine("The workbook has been protected with a password and saved as 'protected_sample.xlsx'.")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation:

  • Install IronXL: You need to ensure that IronXL is installed in your project. This can be done via the NuGet Package Manager by searching for and installing the IronXL package.

  • Loading the Workbook: The WorkBook.Load method is used to load an existing Excel file (sample.xlsx) into memory.

  • Encrypting the Workbook: The Encrypt method is called on the WorkBook object to set a password (iron software). This method encodes the Excel file such that it requires a password to be opened again.

  • Saving the Protected Workbook: The SaveAs method saves the current state of the workbook to a new file, protected_sample.xlsx, which is now password protected.

  • User Notification: A simple console output informs the user that the workbook has been successfully protected and saved.

Further Reading: How to Set a Password to Workbook

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Set a Password on an Excel Worksheet in C#
NEXT >
How to Select a Range in Excel Using C#