Check if the workbook is password protected or not using Aspose.Cells for .NET in C#

I have a workbook which is password protected i.e we need to give password to open that excel file.

I am using aspose.cells to convert excels to pdf .

I need to check through code whether the excel file is pasword protected or not.Below is the code that is being used:

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();

workbook.Open(HttpContext.Current.Server.MapPath(foldername + FileID));

if (workbook.IsProtected==true)

{

//here goes your code for protected file

}

But im getting eroor at the first line itself when it is trying to open the file.

How to check in that case if the excel is password proteced ?

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

I am afraid, there is no way to detect if the file is protected or not before actually loading it.

You must load your file by providing the password only then this property will become valid. If you want to detect it before loading, then you will have to catch the exception thrown by it.

The following sample illustrates how to load the password protected workbook.

C#


//Instantiate LoadOptions

LoadOptions loadOptions = new LoadOptions();


//Specify the password

loadOptions.Password = “007”;


//Create a Workbook object and opening the file from its path

Workbook wb = new Workbook(“e:\test\EncryptedBook.xls”, loadOptions);

Here i will be converting excels to pdf. So for password protected file i dont know the paswords where i need to check and stop the conversion to pdf as password is unknown.

How to just check if an excel is password protected or not

Hi,

Thanks for your posting and using Aspose.

If you will load password protected file without using LoadOptions and providing the password, it will throw exception.

So this exception is the only way to detect if the file is password protected or not.

Otherwise, you cannot detect it. The following exception will occur, when you will load the password protected file.

Exception:
An unhandled exception of type ‘Aspose.Cells.CellsException’ occurred in Aspose.Cells.dll

Additional information: Invalid password.

Thanks for the reply.

exception will stop the application from running . We just wanted toalert the user not to upload any password protected file if it is just by checking if the file is password protected.

If exception is the only way what is the use of having IsProtected property for workbook ?

Hi,

Thanks for your input and using Aspose.Cells.

The IsProtected property helps you to decide which of your workbook objects are normal and which of them are password protected.

Anyway, we will look into your feature request and if possible implement it in our future versions.

We have logged this issue in our database. Once, we will have some update for you, we will let you know asap.

This issue has been logged as CELLSNET-41175.

As a workaround, you should catch the exception and continue your application.

Hi,

Thanks for your posting and using Aspose.Cells.

We have looked into your issue. We cannot implement this feature, you will have to use the exception and try-catch block for this purpose.

Please change your code with try-catch as following

C#


Try

{

Workbook workbook = new Aspose.Cells.Workbook();


workbook.Open(HttpContext.Current.Server.MapPath(foldername + FileID));

}catch

{

}