Check Password to modify using Aspose.Cells
Contents
[
Hide
]
Sometimes, you need to check if the given password matches with the Password to modify programmatically. Aspose.Cells provides WorkbookSettings.WriteProtection.ValidatePassword() method which you can use to check if the given Password to modify is correct or not.
Check Password to modify in Microsoft Excel
You can assign Password to open and Password to modify while creating your workbooks in Microsoft Excel. Please see this screenshot which shows the interface Microsoft Excel provides to specify these passwords.
![]() |
---|
Check Password to modify using Aspose.Cells
The following sample codes load the source Excel file. It has a Password to open as 1234 and Password to modify as 5678. The code first checks if 567 is correct Password to modify and it returns false and then it checks if 5678 is Password to modify and it returns 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-.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); |
Console Output
Here is the Console Output of the above sample code after loading the source Excel file.
Is 567 correct Password to modify: False
Is 5678 correct Password to modify: True