[]
使用此类可以配置如何保护工作簿不被修改,包括建议以只读方式打开工作簿、指定具有写入权限的用户,以及设置修改文件所需的密码。写保护在工作簿于 Excel 应用程序中打开时生效。
此类的实例通常从 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) 此属性返回存储在工作簿写保护设置中的写保留用户名。
WriteProtection protection = workbook.getWriteProtection();
protection.setWriteReservedBy("User");
String userName = protection.getWriteReservedBy();
此属性设置存储在工作簿写保护设置中的写入保留用户名。
WriteProtection protection = workbook.getWriteProtection();
protection.setWriteReservedBy("User");
String userName = protection.getWriteReservedBy();
value - 工作簿的写入保留用户名。不能为null或空字符串。IllegalArgumentException - 如果 value 为 null 或空字符串。启用此选项时,打开工作簿的应用程序可以建议以只读模式打开工作簿。
WriteProtection protection = workbook.getWriteProtection();
protection.setReadOnlyRecommended(true);
boolean readOnlyRecommended = protection.getReadOnlyRecommended();
true;否则为 false。启用此选项时,打开工作簿的应用程序可以建议以只读模式打开工作簿。
WriteProtection protection = workbook.getWriteProtection();
protection.setReadOnlyRecommended(true);
boolean readOnlyRecommended = protection.getReadOnlyRecommended();
value - 如果选中“建议只读”选项,则为 true;否则为 false。
WriteProtection protection = workbook.getWriteProtection();
String password = getPasswordFromSecureSource();
protection.setWritePassword(password);
boolean writeReserved = protection.getWriteReserved();
true;否则为 false。指定非空密码后,工作簿会被标记为受写保护。当工作簿在 Excel 应用程序中打开时,将应用此写保护。传入 null 或空字符串会清除写保护密码并禁用工作簿写保护。
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 - 用于保护工作簿不被修改的密码。不得超过15个字符; null 或空字符串会清除密码并禁用写保护。IllegalArgumentException - 如果 value 长于 15 个字符。使用此方法可以检查密码是否与 setWritePassword(String) 设置的密码匹配。如果工作簿未受写保护,此方法返回 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 - 要验证的密码。true;否则为 false。