使用Aspose.Cells检查修改密码
Contents
[
Hide
]
在 Microsoft Excel 中创建工作簿时,您可以为打开密码和修改密码分配密码。请参阅此屏幕截图,该屏幕截图显示了微软 Excel 提供的界面以指定这些密码。
有时,您需要检查给定密码是否与程序密码匹配。Aspose.Cells提供了 workbook.getSettings().getWriteProtection().validatePassword() 方法,您可以使用它来检查给定的修改密码是否正确。
使用 Aspose.Cells 检查修改密码的 Java 代码
以下示例代码加载了 source Excel 文件。它具有打开密码 1234 和修改密码 5678。代码首先检查 567 是否是正确的修改密码,如果返回 false,然后检查 5678 是否为修改密码,如果返回 true。
This file contains 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.getSharedDataDir(CheckPassword.class) + "articles/"; | |
// Specify password to open inside the load options | |
LoadOptions opts = new LoadOptions(); | |
opts.setPassword("1234"); | |
// Open the source Excel file with load options | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx", opts); | |
// Check if 567 is Password to modify | |
boolean ret = workbook.getSettings().getWriteProtection().validatePassword("567"); | |
System.out.println("Is 567 correct Password to modify: " + ret); | |
// Check if 5678 is Password to modify | |
ret = workbook.getSettings().getWriteProtection().validatePassword("5678"); | |
System.out.println("Is 5678 correct Password to modify: " + ret); |
Java代码生成的控制台输出
这是以上示例代码加载 源Excel 文件后的控制台输出。
Is 567 correct Password to modify: false
Is 5678 correct Password to modify: true