简体   繁体   中英

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.

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?

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. I am working as Support developer/ Evangelist at Aspose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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