[]
        
首页
开发者学堂
文档
论坛
市场
生态机会
活动
立即试用
(Showing Draft Content)

WriteProtection

Class WriteProtection

java.lang.Object
com.grapecity.documents.excel.WriteProtection

public class WriteProtection extends Object
提供对工作簿写保护选项的访问。

使用此类可以配置如何保护工作簿不被修改,包括建议以只读方式打开工作簿、指定具有写入权限的用户,以及设置修改文件所需的密码。写保护在工作簿于 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);
 
  • Constructor Details

    • WriteProtection

      public WriteProtection()
  • Method Details

    • getWriteReservedBy

      public String getWriteReservedBy()
      获取工作簿的写保留用户名。

      此属性返回存储在工作簿写保护设置中的写保留用户名。

      
       WriteProtection protection = workbook.getWriteProtection();
       protection.setWriteReservedBy("User");
       String userName = protection.getWriteReservedBy();
       
      Returns:
      工作簿的写保留用户名。
    • setWriteReservedBy

      public void setWriteReservedBy(String value)
      设置工作簿的写入保留用户名。

      此属性设置存储在工作簿写保护设置中的写入保留用户名。

      
       WriteProtection protection = workbook.getWriteProtection();
       protection.setWriteReservedBy("User");
       String userName = protection.getWriteReservedBy();
       
      Parameters:
      value - 工作簿的写入保留用户名。不能为null或空字符串。
      Throws:
      IllegalArgumentException - 如果 value 为 null 或空字符串。
    • getReadOnlyRecommended

      public boolean getReadOnlyRecommended()
      获取是否选中“建议只读”选项。

      启用此选项时,打开工作簿的应用程序可以建议以只读模式打开工作簿。

      
       WriteProtection protection = workbook.getWriteProtection();
       protection.setReadOnlyRecommended(true);
       boolean readOnlyRecommended = protection.getReadOnlyRecommended();
       
      Returns:
      如果选中“建议只读”选项,则为 true;否则为 false。
    • setReadOnlyRecommended

      public void setReadOnlyRecommended(boolean value)
      设置是否选中“建议只读”选项。

      启用此选项时,打开工作簿的应用程序可以建议以只读模式打开工作簿。

      
       WriteProtection protection = workbook.getWriteProtection();
       protection.setReadOnlyRecommended(true);
       boolean readOnlyRecommended = protection.getReadOnlyRecommended();
       
      Parameters:
      value - 如果选中“建议只读”选项,则为 true;否则为 false。
    • getWriteReserved

      public boolean getWriteReserved()
      获取此工作簿是否受写保护。
      
       WriteProtection protection = workbook.getWriteProtection();
       String password = getPasswordFromSecureSource();
       protection.setWritePassword(password);
       boolean writeReserved = protection.getWriteReserved();
       
      Returns:
      如果 Write Reserved 为真,则为 true;否则为 false。
    • setWritePassword

      public void setWritePassword(String value)
      设置用于工作簿写保护的密码。

      指定非空密码后,工作簿会被标记为受写保护。当工作簿在 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);
       
      Parameters:
      value - 用于保护工作簿不被修改的密码。不得超过15个字符; null 或空字符串会清除密码并禁用写保护。
      Throws:
      IllegalArgumentException - 如果 value 长于 15 个字符。
    • validatePassword

      public boolean validatePassword(String password)
      根据工作簿写保护密码验证指定密码。

      使用此方法可以检查密码是否与 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);
       
      Parameters:
      password - 要验证的密码。
      Returns:
      如果指定密码与工作簿写保护密码匹配,或工作簿未受写保护,则为 true;否则为 false。