We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to check an excel file requires password.

Thread ID:

Created:

Updated:

Platform:

Replies:

105015 Sep 3,2012 04:36 AM Sep 6,2012 07:31 AM Silverlight 3
loading
Tags: XlsIO
Jaeyeol Shin
Asked On September 3, 2012 04:36 AM

Hi. My code to read an excel file is below. 

excelEngine.Excel.Workbooks.Open(stream, ExcelOpenType.Automatic);

This code will occur ArgumentException when reading an encrypted excel file.
How can I check an excel file is encrypted or not?

Johnson Monohar M [Syncfusion]
Replied On September 5, 2012 02:13 AM

Hi Jaeyeol Shin,

Thank you for using Syncfusion products.

XlsIO doesn’t have any property or a method to check whether the file is encrypted or not. However, this can be achieved by an event “OnPasswordRequired” provided at its application level. This event will be raised if an encrypted file is tried to open without its password, where you can provide the password and proceed or else you can stop parsing the workbook.

The following code snippets helps you to achieve this.

private void btnCreateExcel_Click(object sender, System.EventArgs e)

        {

            try

            {

                ExcelEngine excelEngine = new ExcelEngine();

 

                IApplication application = excelEngine.Excel;

                application.OnPasswordRequired += application_OnPasswordRequired;

                application.DefaultVersion = ExcelVersion.Excel2010;

 

                IWorkbook workbook = application.Workbooks.Open(@"../../Data/Sample.xlsx", ExcelOpenType.Automatic);

                IWorksheet sheet = workbook.Worksheets[0];

 

                workbook.Version = ExcelVersion.Excel2010;

                string fileName = @"../../Output/Output.xlsx";

 

                workbook.SaveAs(fileName);

 

                workbook.Close();

                excelEngine.Dispose();

 

                System.Diagnostics.Process.Start(Path.GetFullPath(fileName));

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message.ToString());

            }

            this.Close();

        }

 

void application_OnPasswordRequired(object sender, PasswordRequiredEventArgs e)

{

e.NewPassword = "syncfusion";

}



In addition, we have provided you a sample with the above code snippets for your reference in the following link.

Sample Link : EncryptedExcel.zip

Kindly refer to the provided solution and let us know whether the provided solution has resolved your issue or if you need any clarification.

Thanks,
Johnson


Jaeyeol Shin
Replied On September 5, 2012 04:19 AM

Thank you Johnson.

Is there any idea to distinguish an ArgumentException caused by missing password?

Johnson Monohar M [Syncfusion]
Replied On September 6, 2012 07:31 AM

Hi Jaeyeol Shin,

It is not possible to distinguish ArgumentException for missing password. Only by the provided event in the previous post, we can be able to identify for missing password.

Please let me know if you have any other queries.

Thanks,
Johnson

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;