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. Image for the cookie policy date

How to check an excel file requires password.

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?

3 Replies

JM Johnson Monohar M Syncfusion Team September 5, 2012 06:13 AM UTC

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



JS Jaeyeol Shin September 5, 2012 08:19 AM UTC

Thank you Johnson.

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


JM Johnson Monohar M Syncfusion Team September 6, 2012 11:31 AM UTC

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

Loader.
Live Chat Icon For mobile
Up arrow icon