We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to check invalid password is used to open an encrypted excel file


I have a file with password protected. i am using below code to open the file

LoadOptions loadOptions2 = new LoadOptions(FileFormatType.EXCEL_97_TO_2003);
workbook = new Workbook(xlsFile,loadOptions2);

How to check if the supplied password by user is not valid? this is not giving an exception when a wrong password is used, rather, after this when i am trying to open the worksheet like below.

Worksheet worksheet = workbook.getWorksheets().get(0);

I am getting java.lang.IndexOutOfBoundsException: Index: 0, Size: 0.

I am expecting an exception when the password supplied is not correct instead of an empty worksheet.

Is there a way to achieve this? Thanks in advance!


Thank you for contacting support. If an Excel file is password protected, then the constructor of Workbook class throws an invalid password error on opening an Excel file with incorrect password. Please download and try the latest version 17.3.0 of Aspose.Cells for Java API and let us know how that goes on your side. Aspose.Cells API also allows to verify password used to protect the Worksheet of an Excel file. Please refer to this help topic: Verify Password Used to Protect the Worksheet


In addition to the above reply, you can also check if the given Password to modify is correct or not. Please refer to this help topic: Check Password to modify using Aspose.Cells


Thanks for the pointers.

Using the new version of cells (17.3.0) now. I am still not receiving the error on invalid password.
below is the code again.

1) LoadOptions loadOptions2 = new LoadOptions(FileFormatType.EXCEL_97_TO_2003);
2) loadOptions2.setPassword(_fileKey);
3) workbook = new Workbook(xlsFile, loadOptions2);

4) Worksheet worksheet = workbook.getWorksheets().get(0);

You mean i should get a Invalid Password error on the above line 4? or in line 3 ?

I think i have problem understanding what is Opening of the workbook means.

Also does is matter using load option EXCEL_97_TO_2003? I am using Excel version 2010 to test, but i need the code to support all versions of excel.


Thank you for the inquiry. The file opening means importing an Excel file into the Aspose.Cells API. Your third line of code is importing an Excel file. Please send us your source Excel file and password for the testing purposes. It will help us to replicate the same error on our side. Once the error is reproducible on our side, then we would be able to fix it. We shall investigate and reply you appropriately. Your response is awaited.

Please also see how to send the confidential files to a staff member via an email: Send your License File to an Aspose Staff Member


Thank you for sending the source Excel file and password. We managed to replicate the said problem on our side. It has been logged under ticket ID CELLSNET-45236 in our bug tracking system. Your post has also been linked to this ticket. We shall keep you informed regarding any available updates. We are sorry for the inconvenience caused.


Thank you for being patient. In reference to the ticket ID CELLSNET-45236, you have send us an XLSX file via an email and in the code it appears as you are loading an XLS file. Anyways, please use LoadFormat class in the LoadOptions class constructor, instead of the FileFormatType class. Using the next version 17.4.0, Aspose.Cells API will throw an error on loading XLSX file with LoadFormat.EXCEL_97_TO_2003 option. For loading an XLSX file, you will use LoadFormat.XLSX. You may also not set the load format in LoadOptions class constructor. Aspose.Cells API can auto-detect the file format. Our product team has resolved this ticket. If there is no issue in the QA phase, then this fix will be included in the next version 17.4.0 of Aspose.Cells API. We shall notify you via this forum thread as soon as the new release is published.


Please try our latest version/fix: Aspose.Cells for .NET v17.3.3.

Aspose.Cells for .NET v17.3.3 (.NET 2.0)
Aspose.Cells for .NET v17.3.3 (.NET 4.0)
(Note: please choose any of the fixes for your underlying .NET framework version)

Your issue should be fixed in it.

Let us know your feedback.

Thank you.

The issues you have found earlier (filed as CELLSNET-45236) have been fixed in Aspose.Cells for .NET 17.4.0.

This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

I am use version for .Net Standard and am experiencing the same issue. I am not getting an exception being thrown for an invalid password. Has this bug been identified in the .Net Standard version as well? Is there a work around to determine if a bad password is used?


Could you provide a sample project to reproduce the issue, you may zip the project and attach it here. Also include template file(s) that you are using?

Turns out it was an issue with my unit test order. I ran a unit test to validate a good password worked, which unprotected the file, such that when I tried the bad password it still worked because the file had already been unprotected. I added a test initialize to restore the files at the start of each test and it is now working as expected. Sorry for the confusion.


Good to know that you have sorted it out now. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.