验证用于保护工作表的密码

验证用于保护工作表的密码

Protection.verifyPassword 方法返回true,如果指定的密码与保护给定工作表所使用的密码匹配,则返回false,如果指定的密码不匹配。以下代码片段使用 Protection.verifyPassword 方法与 Protection.isProtectedWithPassword 属性结合使用以检测密码保护,并验证密码。

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