Verify Password Used to Protect the Worksheet
Contents
[
Hide
]
Aspose.Cells APIs have enhanced the Protection class by introducing some useful properties & methods. One such method is the verifyPassword which allows specifying a password as an instance of String and verifies if the same password has been used to protect the Worksheet.
Verify Password Used to Protect the Worksheet
The Protection.verifyPassword method returns true if the specified password matches with the password used to protect the given worksheet, false if the specified password does not match. Following piece of code uses the Protection.verifyPassword method in conjunction with Protection.isProtectedWithPassword property to detect the password protection, and verifies the password.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(VerifyPasswordtoProtectWorksheet.class); | |
// Create an instance of Workbook and load a spreadsheet | |
Workbook book = new Workbook(dataDir + "book1.xlsx"); | |
// Access the protected Worksheet | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Check if Worksheet is password protected | |
if (sheet.getProtection().isProtectedWithPassword()) { | |
// Verify the password used to protect the Worksheet | |
if (sheet.getProtection().verifyPassword("password")) { | |
System.out.println("Specified password has matched"); | |
} else { | |
System.out.println("Specified password has not matched"); | |
} | |
} |