This simple article explains how to open password protected Excel file in C# by providing necessary steps and a runnable sample code. By using C# open Excel with password feature can be implemented with the help of a couple of lines only, for any type of supported Excel files say XLSX, XLS, ODS etc. You can control the application flow properly by catching the exceptions raised in case of missing passwords or wrong passwords.
Steps to Open Password Protected Excel File in C#
- Configure the project to add Aspose.Cells from the NuGet package manager
- Create LoadOptions class object to use the password property
- Set the password required to open the protected Excel file
- Load the protected Excel file into the Workbook object using the LoadOptions object with a password
These steps explain how C# open password protected Excel file with the help of a sequence of operations which first include ths step to add necessary libraries. Then by using the LoadOptions class object which contains the Password property the Excel workbook password will be set. This single property needs to be filled before opening the protected file by using the Workbook class constructor that requires the file name and the LoadOptions object.
Code to Load Password Protected Excel File using C#
In this code, if you do not fill out the password or set this property to null, you will get the exception “Please provide password for the Workbook file.” and if you provide the wrong password, you will get the exception “Invalid password”. Note that after successfully opening the protected file, if you set this property to null, the password will be removed permanently when saved on disk with the same name or as a different file.
This article has guided us how using C# open Excel file with password. If you want to learn to save the Excel file as a PDF file having restrictions, refer to the article on how to convert XLSX to PDF having restricted permissions using C#.