How to Open Password Protected Excel File in Java

This simple topic encompasses how to open password protected Excel file in Java by covering necessary steps and a runnable sample code. By using Java open Excel with password feature can be developed with the help of a few lines only, for any type of supported Excel files including XLSX, XLS, ODS, etc. One can easily control the application flow properly by handling the exceptions raised in case of missing passwords or wrong passwords.

Steps to Open Password Protected Excel File in Java

  1. Configure the project to add Aspose.Cells from the repository manager
  2. Create an instance of the LoadOptions class object to use the password property
  3. Set the password of the required Excel file
  4. Access the protected Excel file using an instance of the Workbook class object using the LoadOptions object with a password

The aforementioned steps explain how Java open password protected Excel file using a well defined sequence of operations which will be commenced by including the step to add the necessary JAR file. Then by using an instance of the LoadOptions class object which contains the setPassword() method, the Excel workbook password will be set. This single method value needs to be set before accessing 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 Java

In this example, if you do not provide the password or set the method value to null, an exception with a message stating, “Please provide a password for the Workbook file”. If you set a wrong password, you will get the exception “Invalid password”. It is important to note that once after successfully opening the protected file if you set the password of the loaded Excel file to Null using setPassword(), it will remove the password protection permanently from the file when saved on disk with the same name or as a different file.

This topic has focused on how using Java open Excel file with password. If you want to learn about removing a pivot table inside the Excel file, refer to the article on How to Remove Pivot Table using Java.

 English