Search code examples
c#epplus

Trying to read an Excel file from ZipArchive with EPPlus


I'm trying to read an excel file from a zip archive:

var excelEntry = archive.Entries.Single(entry => Regex.IsMatch(entry.FullName, @"\.xlsx\z", RegexOptions.IgnoreCase));
using (var excelPackage = new ExcelPackage(excelEntry.Open()))
{
}

However, I get a NotSupportedException.

(Exception thrown: 'System.NotSupportedException' in System.dll
Additional information: This operation is not supported.) 

The file is a simple .xlsx without any protection. Any ideas?


Solution

  • It was indeed due to the fact that the zip archive stream was read only, whereas the ExcelPackage only accepts read/write streams.

    I made the following adjustments:

    var entryStream = excelEntry.Open();
    using (var ms = new MemoryStream())
    using (var excelPackage = new ExcelPackage())
    {
      entryStream.CopyTo(ms);
      excelPackage.Load(ms);
      ...
    }