BILL
BILL

Reputation: 4869

How to check whether it is password-protected excel file using EPPlus?

When I trying open secured document I have this exception

System.Exception : Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password

I write this hard method for test

public bool HasPassword()
        {
            try
            {
                if(File.Exists(FileName))
                {
                    var fileStream = File.Open(FileName, FileMode.Append);
                    var package = new ExcelPackage();
                    package.Load(fileStream);
                }
            }
            catch(Exception)
            {
                return true;
            }
            return false;
        }

but I think that it is wrong approach.

How to check whether it is password-protected excel file ?

Upvotes: 1

Views: 4369

Answers (1)

jAC
jAC

Reputation: 5324

If EEPlus isn't mandatory you can simply use the Workbook.HasPassword property.

Workbook book = ****xyz****;
if (book.HasPassword)
{
    book.Password = Properties.Settings.Default.ExcelFilePW;
    MessageBox.Show("Excel file is encrpyted");
}

Therefore you need to create a reference to the Office Interop Excel component which can be found in .NET / COM (sometimes). Then just embed it in your project with the using directive.

Upvotes: 2

Related Questions