简体   繁体   English

Aspose.Cells 安全 Excel 文件中的密码(c#)

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

I want to check if the given Password is the password which is locking the Excel-File.我想检查给定的密码是否是锁定 Excel 文件的密码。

Here is how i tried it:这是我尝试的方法:

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;
    }

How can i fix this, so that if the EmployeeGrid's (set as a Workbook property) password isn't the same as the given password the MessageBox get showed and the code leaves the method?我该如何解决这个问题,以便如果 EmployeeGrid 的(设置为 Workbook 属性)密码与给定的密码不同,MessageBox 会显示并且代码会离开该方法?

Your code looks to me OK, what's wrong with it?您的代码在我看来不错,有什么问题? Your current code is trying to load an encrypted (password protected) file with the given password.您当前的代码正在尝试使用给定密码加载加密(受密码保护)文件。 If your code is not doing what you want, kindly do elaborate your needs, so we could help you accordingly.如果您的代码没有按照您的意愿行事,请详细说明您的需求,以便我们可以为您提供相应的帮助。 I will also write another sample code for your complete reference: eg我还将编写另一个示例代码供您完整参考:例如

Sample code:示例代码:

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. PS。 I am working as Support developer/ Evangelist at Aspose.我在 Aspose 担任支持开发人员/传播者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM