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

Free Support Forum - aspose.com

Detect password protection for the MS Excel document

Hello Aspose.Cells team!

I have a question about opening the encrypted password-protected Excel documents using Aspose.Cells for .NET. I know that it is possible to open the password-protected documents by specifying the password in the “LoadOptions” class and then specify this class instance into the “Workbook” constructor. Also, if an Excel file is encrypted, and password was not specified or valid, Aspose.Cells throws an exception.

My question is the next. Is it possible to define, a specific document contain a password protection or not, without opening it as a “Workbook” instance and waiting for the exception? Also, is it posisble to detect, if a specified password is valid for specific encrypted Excel document?

Aspose.Pdf contains such functionality and I’m wondering is there something similar in case of Aspose.Cells.

Thanks in advance.

With best regards,
Denis Gvardionov

Hi,


Thanks for providing us some details.

1) Well, using Aspose.Cells APIs, you may detect if a file is password protected (encrypted) or not. You may use FileFormatInfo.IsEncrypted Boolean attribute for the task. See the sample code below:
e.g
Sample code:

//Detect File format
FileFormatInfo info = FileFormatUtil.DetectFileFormat(“e:\test2\Book1.xlsx”);
//Check if the file is encrypted or not.
Console.WriteLine("The file is encrypted: " + info.IsEncrypted);

Please see the document for your reference here:
http://www.aspose.com/docs/display/cellsnet/How+to+Detect+a+File+Format+and+Check+if+the+File+is+Encrypted


2) Please see the following sample code that demonstrates on how to keep loading a workbook until it found the correct password for opening the file. You may refer to it and may update/ amend it and write your own code accordingly for your needs.

e.g
Sample code:

string filePath = “e:\test2\Book1.xlsx”;


string[] passwordList = { “222”, “333”, “123”, “1234”, “4444” };


Workbook workbook = null;


for (int i = 0; i < passwordList.Length; i++)
{

LoadOptions loadOpts = new LoadOptions();

loadOpts.Password = passwordList[i];


try
{

workbook = new Workbook(filePath, loadOpts);

}

catch (Exception ex)
{

workbook = null;

}


if (workbook != null)

break;

}


if (workbook == null)

Debug.WriteLine(“All passwords were wrong.”);


if (workbook != null)

Debug.WriteLine(“Workbook loaded successfully.”);

Hope, this helps a bit.

Thank you.

Hello Amjad,

Thanks for super-fast response, it was very helpful and useful.

With best regards,

Denis Gvardionov

Hi Denis,


You are most welcome. Please feel free to contact us back in case you need our further assistance with Aspose APIs.