WorkbookBase.Password 属性

获取或设置打开工作簿时必须提供的密码。

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel.v4.0.Utilities(在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)

语法

声明
Public Property Password As String
    Get
    Set
public string Password { get; set; }

属性值

类型:System.String
打开工作簿时必须提供的密码。

备注

使用由大写字母和小写字母、数字和符号组合而成的强密码。 弱密码不混合使用这些元素。 强密码:Y6dh!et5。 弱密码:House27。 使用一个方便记忆的强密码,这样就不必将它记在纸上。

示例

下面的代码示例检查 HasPassword 属性的值,以确定工作簿是否受密码保护。 如果工作簿不受密码保护,则此示例将 Password 属性设置为从用户输入获得密码,然后调用 SetPasswordEncryptionOptions 方法设置加密算法、密钥长度和加密提供程序的名称,并启用文件属性加密。 然后,此示例在工作表 Sheet1 中显示 PasswordEncryptionProviderPasswordEncryptionAlgorithmPasswordEncryptionKeyLengthPasswordEncryptionFileProperties 属性的值。 此示例假定方法 GetPasswordFromUserInput 是在别处定义的。

此示例针对的是文档级自定义项。

Private Sub WorkbookPasswordOptions()
    If Not Me.HasPassword Then
        Me.Password = GetPasswordFromUserInput()
        Me.SetPasswordEncryptionOptions( _
            "Microsoft RSA SChannel Cryptographic Provider", _
            "RC4", 128, True)
    End If

    ' Display the password properties in Sheet1.
    Globals.Sheet1.Range("A1").Value2 = _
        "Password Encryption Provider:"
    Globals.Sheet1.Range("A2").Value2 = _
        "Password Encryption Algorithm:"
    Globals.Sheet1.Range("A3").Value2 = _
        "Password Encryption Key Length:"
    Globals.Sheet1.Range("A4").Value2 = _
        "Password Encryption File Properties:"
    Globals.Sheet1.Range("A1", "A4").Columns.AutoFit()

    Globals.Sheet1.Range("B1").Value2 = _
        Me.PasswordEncryptionProvider
    Globals.Sheet1.Range("B2").Value2 = _
        Me.PasswordEncryptionAlgorithm
    Globals.Sheet1.Range("B3").Value2 = _
        Me.PasswordEncryptionKeyLength
    Globals.Sheet1.Range("B4").Value2 = _
        Me.PasswordEncryptionFileProperties
    Globals.Sheet1.Range("B1", "B4").Columns.AutoFit()
End Sub
private void WorkbookPasswordOptions()
{
    if (!this.HasPassword)
    {
        this.Password = GetPasswordFromUserInput();
        this.SetPasswordEncryptionOptions(
            "Microsoft RSA SChannel Cryptographic Provider",
            "RC4", 128, true);
    }

    // Display the password properties in Sheet1.
    Globals.Sheet1.Range["A1", missing].Value2 =
        "Password Encryption Provider:";
    Globals.Sheet1.Range["A2", missing].Value2 =
        "Password Encryption Algorithm:";
    Globals.Sheet1.Range["A3", missing].Value2 =
        "Password Encryption Key Length:";
    Globals.Sheet1.Range["A4", missing].Value2 =
        "Password Encryption File Properties:";
    Globals.Sheet1.Range["A1", "A4"].Columns.AutoFit();

    Globals.Sheet1.Range["B1", missing].Value2 =
        this.PasswordEncryptionProvider;
    Globals.Sheet1.Range["B2", missing].Value2 =
        this.PasswordEncryptionAlgorithm;
    Globals.Sheet1.Range["B3", missing].Value2 =
        this.PasswordEncryptionKeyLength;
    Globals.Sheet1.Range["B4", missing].Value2 =
        this.PasswordEncryptionFileProperties;
    Globals.Sheet1.Range["B1", "B4"].Columns.AutoFit();
}

.NET Framework 安全性

请参见

参考

WorkbookBase 类

Microsoft.Office.Tools.Excel 命名空间