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

Free Support Forum - aspose.com

How to check if the workbook/worksheet is protected with a password or not

Dear Team,

We have a requirement to identify weather a workbook and worksheet is protected with password or empty password. Do we have any methods in Aspose for this.

we have tried the following and is working perfectly if this has a valid password

If oBook.Worksheets("ABC").IsProtected Then

oBook.Worksheets("ABC").Unprotect(p_strPassword)

End If

Now we have issue with workbook/worksheet protected with empty password. Is there a way by which we can identify if the password is empty or has a valid length. We tried

oBook.Settings.Password --> this is always giving nothing

Thanks
Anish

Hi Anish,

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

MS-Excel does not allow to save Excel workbooks with empty password. So password must be non-empty.

In order to create a workbook object from encrypted xls/xlsx file, you must supply the password in LoadOption as a parameter. Please see the following code

//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);

Once, workbook object is created successfully, then Workbook.Settings.Password will contain a valid password

You can also detect if the Excel workbook is password protected or not without loading it. You can use FileFormatInfo.IsEncrypted property to detect if the file is encrypted or not.

Please see the following documentation article that illustrate how to detect the format of the file and check if the file is encrypted or not.



Thanks for the reply

What about the worksheet. We can protect the worksheet without giving any password. Is there a way by which I can identify if the sheet is protected with a empty/non-empty password?

Thanks
Anish

Hi Anish,

Thanks for your posting and using Aspose.Cells.

Aspose.Cells provides a method Worksheet.UnProtect() without any parameter, this method can be used to detect if the worksheet is protected with password or with empty password.

If the worksheet is protected with empty password, this method will unprotect the worksheet and if it is protected with some password, it will throw exception.

You can first check, if the worksheet is protected using Worksheet.IsProtected property, then you can try to unprotect it with Worksheet.UnProtect() method and see if it throws exception, if it does not throw exception, then it means, worksheet was protected with empty password. Please see the following code for your reference.

C#


Workbook workbook = new Workbook(“source.xlsx”);


Worksheet worksheet = workbook.Worksheets[1];


if (worksheet.IsProtected == true)

{

worksheet.Unprotect();


if (worksheet.IsProtected == false)

Console.WriteLine(“Worksheet was protected with empty password.”);

}

Dear Team,

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

MS-Excel does not allow to save Excel workbooks with empty password. So password must be non-empty.

I am confused. Using MS-Excel we are able to save excel with empty password. (See the screen shot attached). Also Aspose is giving oBook.Settings.IsProtected as true for the workbooks which is having empty password.

I still stand in my first question. How I can identify if the workbook is protected with a empty password or a non-empty password

Thanks
Anish

Hi Anish,

Thanks for your further feedback and using Aspose.Cells.

You will use the same logic to detect if the workbook is protected with empty password or not as mentioned in the above post.

Please see the following code for your reference. It loads a workbook which is protected by empty password. It then unprotect it using Workbook.Unprotect() method and then checks Workbook.IsProtected property to check if it is protected or not. If it is unprotected, then it means it was protected with empty password before.

C#


Workbook workbook = new Workbook(“source.xlsx”);


if (workbook.Settings.IsProtected == true)

{

workbook.Unprotect("");


if (workbook.IsProtected == false)

Console.WriteLine(“Workbook was protected with empty password.”);

}

I believe there may be a bug, I am using apose-cells-jdk6 8.4.2
I have a spreadsheet where there is a password for reading the document and a password for editing the document.
When using FileFormatUtil, I give it the attached file and isEncrytped() returns false but then when I try to create worksheet object is throws an error:
Caused by: com.aspose.cells.CellsException: Please provide password for the Workbook file.

Ps. The password for the file is in brackets in the filename.


Bug is not present in latest version (16.11.0).






Hi Priyesh,


Thanks for the template file and details.

Well, it might be a bug in the older version that you are using because it works fine using our latest version/fix: Aspose.Cells for Java v17.5.7 (attached) - please try it (there is no alternative to cope with your issue except upgrading to latest APIs set). I used the following sample code using v17.5.7 with your attached file and it gives correct console output:
e.g.
Sample code:

// Detect file format
FileFormatInfo info = FileFormatUtil.detectFileFormat(“password-protected-spreadsheet(letmein).xls”);

// Gets the detected load format
System.out.println("The spreadsheet format is: " + FileFormatUtil.loadFormatToExtension(info.getLoadFormat()));

// Check if the file is encrypted.
System.out.println("The file is encrypted: " + info.isEncrypted());

Console output:
The spreadsheet format is: .xls
The file is encrypted: true

Thank you.

Thank you for your fast reply.

I have build with 16.11.0 and bug is not present.

Thanks again,

Hi,


Good to know that v16.11.0 fixes your issue. Feel free to contact us any time if you have further comments or questions, we will be happy to assist you soon.

Thank you.