How to open password protected Excel File with NPOI in C#

Viewed 1933

Good day!

Currently the code to open excel file is:

public void LoadExcelFile(string fullPath)
{
    using (var fileStream = File.OpenRead(fullPath))
    {
        _workbook = WorkbookFactory.Create(fileStream);
    }
}

One of the files that I need to open now is password protected.
How can I send in a password to open the file?

Using NPOI version 2.3.0.0

Thank you in advance!

1 Answers

Found a solution:

As mentioned above, NPOI does not cater for a file with a password.

So I added a reference through NuGet to EPPlus and calling it as follows:

    public void LoadExcelFile(string fullPath, string password)
    {

        var file = new FileInfo(fullPath);
        var _workbook = new OfficeOpenXml.ExcelPackage(file, "password").Workbook;
     }

Using it in OutSystems to load Excel files with various formats.

Related