Open password protected excel document using Aspose.Cells

Hi,

I am using Aspose.Cells to read contents from Excel workbook. I am using below code to open the password protected documents using Aspose.Cells.

try

{

Workbook _workBook = new Workbook(m_documentFile);

}

catch (Exception ex)

{

if (ex.Message.CompareTo("Invalid password.") == 0)

{

LoadOptions loadOps = new LoadOptions();

loadOps.Password = m_passwordList[0] as string;

_workBook = new Workbook(m_documentFile,loadOps);

}

}

But my current requirement is to loop the list of passwords provided by the user and try to open the document utile the document opens sccessfully.

Please provide the sample code for this as soon as possible.

Thanks,

Dhivya

Hi Dhivya,

Thanks for using Aspose.Cells.

Please see the following code. It iterates all the password list and tries to open the workbook one by one with each password. If the workbook is loaded successfully, it prints a message “Workbook loaded successfully” and if none of the password worked, then it prints a message "All passwords are wrong."

C#


string filePath = “F:\Shak-Data-RW\Downloads\Book1.xlsx”;


string[] passwordList = {“222”, “333”, “123”, “1234”, “4444” };


Workbook workbook = null;


for (int i = 0; i < passwordList.Length; i++)

{

LoadOptions loadOpts = new LoadOptions();

loadOpts.Password = passwordList[i];


try

{

workbook = new Workbook(filePath, loadOpts);

}

catch (Exception ex)

{

workbook = null;

}


if (workbook != null)

break;

}


if (workbook == null)

Debug.WriteLine(“All passwords were wrong.”);


if(workbook !=null)

Debug.WriteLine(“Workbook loaded successfully.”);


Hi,


I have written another sample code for your reference for your needs. I have stored some passwords in an array list. The code will first check if the underlying file is encrypted and then trying loop through each password in the list to try to open the file by Aspose.Cells APIs. Once the password is matched and found, it opens the file successfully and exit the loop etc.
e.g
Sample code:

string m_documentFile = “e:\test\EncryptedBook1.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;

}

}

Hope, this helps a bit.

Thank you.

Hi Amjad Sahi is not online. Last active: 04-15-2014, 2:18 PMAmjad Sahi,

Thank you for your sample code.

Hi,


You are welcome, hopefully the code segments would help you to accomplish your tasks. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

Thank you.