验证用于保护工作表的密码
Contents
[
Hide
]
Aspose.Cells API通过引入一些有用的属性和方法来增强Protection类。 其中一个方法是VerifyPassword,允许将密码作为string的实例进行指定,并验证是否已经使用相同的密码来保护Worksheet。
Protection.VerifyPassword方法将返回true,如果指定的密码与用于保护给定工作表的密码匹配,并且如果指定的密码不匹配,则返回false。 下面的代码片段使用Protection.VerifyPassword方法与Protection.IsProtectedWithPassword属性结合使用,以检测密码保护,并验证密码。
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create an instance of Workbook and load a spreadsheet | |
var book = new Workbook(dataDir + "Sample.xlsx"); | |
// Access the protected Worksheet | |
var sheet = book.Worksheets[0]; | |
// Check if Worksheet is password protected | |
if (sheet.Protection.IsProtectedWithPassword) | |
{ | |
// Verify the password used to protect the Worksheet | |
if (sheet.Protection.VerifyPassword("1234")) | |
{ | |
Console.WriteLine("Specified password has matched"); | |
} | |
else | |
{ | |
Console.WriteLine("Specified password has not matched"); | |
} | |
} |