Aspose.Cells 安全密码 Excel-FIle (c#)

Aspose.Cells Password in secured Excel-FIle (c#)

我想检查给定的密码是否是锁定 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 并且代码离开该方法?

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

示例代码:

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 担任支持开发人员/传播者。