使用 Aspose.Cells 检查修改密码
Contents
[
Hide
]
有时,您需要检查给定的密码是否与修改密码以编程方式。 Aspose.Cells 提供 WorkbookSettings.WriteProtection.ValidatePassword() 方法,您可以使用该方法检查给定的要修改的密码是否正确。
在Microsoft Excel中勾选修改密码
你可以分配打开密码和修改密码在 Microsoft Excel 中创建工作簿时。请查看此屏幕截图,其中显示了 Microsoft Excel 提供的用于指定这些密码的界面。
![]() |
---|
使用 Aspose.Cells 检查修改密码
以下示例代码加载源Excel文件。它的打开密码为 1234,修改密码为 5678。代码首先检查 567 是否为正确的修改密码并返回 false,然后检查 5678 是否为修改密码并返回 true。
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Specify password to open inside the load options | |
LoadOptions opts = new LoadOptions(); | |
opts.Password = "1234"; | |
// Open the source Excel file with load options | |
Workbook workbook = new Workbook(dataDir + "sampleBook.xlsx", opts); | |
// Check if 567 is Password to modify | |
bool ret = workbook.Settings.WriteProtection.ValidatePassword("567"); | |
Console.WriteLine("Is 567 correct Password to modify: " + ret); | |
// Check if 5679 is Password to modify | |
ret = workbook.Settings.WriteProtection.ValidatePassword("5678"); | |
Console.WriteLine("Is 5678 correct Password to modify: " + ret); |
控制台输出
这是加载后上述示例代码的控制台输出源Excel文件。
Is 567 correct Password to modify: False
Is 5678 correct Password to modify: True