[]
Use this class to configure how a workbook is protected from modification, including recommending that the workbook be opened as read-only, specifying the user who has write permission, and setting a password required to modify the file. The write-protection takes effect when the workbook is opened in an Excel application.
Instances of this class are typically obtained from Workbook.getWriteProtection().
WriteProtection protection = workbook.getWriteProtection();
protection.setWriteReservedBy("User");
protection.setReadOnlyRecommended(true);
// Get the password from user input via getPasswordFromSecureSource().
String password = getPasswordFromSecureSource();
protection.setWritePassword(password);
String fileName = "WriteProtection.xlsx";
workbook.save(fileName);
booleanbooleanvoidsetReadOnlyRecommended(boolean value) voidsetWritePassword(String value) voidsetWriteReservedBy(String value) booleanvalidatePassword(String password) This property returns the write-reserved user name stored in the workbook write protection settings.
WriteProtection protection = workbook.getWriteProtection();
protection.setWriteReservedBy("User");
String userName = protection.getWriteReservedBy();
This property sets the write-reserved user name stored in the workbook write protection settings.
WriteProtection protection = workbook.getWriteProtection();
protection.setWriteReservedBy("User");
String userName = protection.getWriteReservedBy();
value - The write-reserved user name for the workbook. Must not be null or an empty string.IllegalArgumentException - if value is null or an empty string.When this option is enabled, applications that open the workbook can recommend opening it in read-only mode.
WriteProtection protection = workbook.getWriteProtection();
protection.setReadOnlyRecommended(true);
boolean readOnlyRecommended = protection.getReadOnlyRecommended();
true if the Read Only Recommended option is selected; otherwise, false.When this option is enabled, applications that open the workbook can recommend opening it in read-only mode.
WriteProtection protection = workbook.getWriteProtection();
protection.setReadOnlyRecommended(true);
boolean readOnlyRecommended = protection.getReadOnlyRecommended();
value - true if the Read Only Recommended option is selected; otherwise, false.
WriteProtection protection = workbook.getWriteProtection();
String password = getPasswordFromSecureSource();
protection.setWritePassword(password);
boolean writeReserved = protection.getWriteReserved();
After a non-empty password is specified, the workbook is marked as write protected. This write protection is applied when the workbook is opened in an Excel application. Passing null or an empty string clears the write-protection password and disables workbook write protection.
WriteProtection protection = workbook.getWriteProtection();
// Get the password from user input via getPasswordFromSecureSource().
String password = getPasswordFromSecureSource();
protection.setWritePassword(password);
boolean writeReserved = protection.getWriteReserved();
boolean passwordMatches = protection.validatePassword(password);
value - The password used to protect the workbook from modification. Must not exceed 15 characters; null or an empty string clears the password and disables write protection.IllegalArgumentException - if value is longer than 15 characters.Use this method to check whether a password matches the password set by setWritePassword(String). If the workbook is not write protected, this method returns true.
WriteProtection protection = workbook.getWriteProtection();
// Get the password from user input via getPasswordFromSecureSource().
String password = getPasswordFromSecureSource();
protection.setWritePassword(password);
boolean isValid = protection.validatePassword(password);
password - The password to validate.true if the specified password matches the workbook write-protection password, or if the workbook is not write protected; otherwise, false.