【问题标题】:Aspose.Cells Password in secured Excel-FIle (c#)Aspose.Cells 安全 Excel 文件中的密码(c#)
【发布时间】:2019-11-11 11:46:36
【问题描述】:

我想检查给定的密码是否是锁定 Excel 文件的密码。

我是这样尝试的:

var DepartmentStream = new FileStream(Deparment, FileMode.Open);
var EmplyoeeStream = new FileStream(Employee, FileMode.Open);

var options = new LoadOptions {Password = "ExamplePassword123"};

DepartmentGrid = new Workbook(DepartmentStream); // set as a Workbook property and doesn't need a password

try
    {
     EmployeeGrid = new Workbook(EmplyoeeStream , options); // set as a Workbook property
    }
catch (Exception ex)
    {
     EmployeeGrid= null;
    }

if (EmployeeGrid == null)
    {
     MessageBox.Show("The given password is wrong!", "Wrong password",);
     return;
    }

我该如何解决这个问题,如果 EmployeeGrid 的(设置为工作簿属性)密码与给定密码不同,则会显示 MessageBox 并且代码会离开该方法?

【问题讨论】:

    标签: c# excel aspose aspose-cells


    【解决方案1】:

    您的代码在我看来不错,有什么问题?您当前的代码正在尝试使用给定密码加载加密(受密码保护)文件。如果您的代码没有按照您的意愿行事,请详细说明您的需求,以便我们可以为您提供相应的帮助。我还将编写另一个示例代码供您完整参考: 例如

    示例代码:

    string m_documentFile = "e:\\test2\\EncryptedBook1j.xls";
                //Get the password list to store into arrays.
                string[] m_passwordList = { "001", "002", "003", "004", "005", "006", "007", "008" };
                Workbook _workBook;
                int i = 0;
                int ncnt = 0;
    
                //Check if the file is password protected.
                FileFormatInfo fft = FileFormatUtil.DetectFileFormat(m_documentFile);
                bool check = fft.IsEncrypted;
            RetryLabel:
                try
                {
                    if (check)
                    {
    
                        LoadOptions loadOps = new LoadOptions();
    
                        for (i = ncnt; i < m_passwordList.Length; i++)
                        {
    
                            loadOps.Password = m_passwordList[i];
                            _workBook = new Workbook(m_documentFile, loadOps);
                            MessageBox.Show("Opened Successfully with the Password:" + m_passwordList[i]);
                            break;
    
    
                        }
    
                    }
                }
                catch (Exception ex)
                {
    
                    if (ex.Message.CompareTo("Invalid password.") == 0)
                    {
    
                        MessageBox.Show("Invalid Password: " + m_passwordList[i] + " ,trying another in the list");
                        ncnt = i + 1;
                        goto RetryLabel;
    
                    }
    
    
                }
    

    PS。我在 Aspose 担任支持开发人员/布道者。

    【讨论】:

      猜你喜欢
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多