Search code examples
c#linuxasp.net-coreepplus

ASP.NET CORE and EPPLUS library on Linux - IndexOutOfRangeException: Worksheet position out of range


Please note that I have found several existing topics with this problem (EPPLUS IndexOutOfRangeException: Worksheet position out of range.), but none that were specific only to Linux publish.

We have an ASP.NET CORE 3.1 web project with EPPlus 5.1.2 library.

For some reports we open a pre-existing Excel template (.xlsx), fill it with some data and return to the user for download. Everything works perfectly when Debugging or when we publish the web app to a Windows Server.

But, if we publish to our Linux server (Debian 10 64 Bit) we get the following error when trying to generate the report:

An unhandled exception occurred while processing the request.
IndexOutOfRangeException: Worksheet position out of range.

OfficeOpenXml.ExcelWorksheets.get_Item(int PositionID)

Otherwise everything else seems to work fine (on Linux).

Report generating code:

MemoryStream result;

FileInfo fileInfo = new FileInfo(Path.Combine(path, "template.xlsx"));
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

using (ExcelPackage xlPackage = new ExcelPackage(fileInfo))
{
    ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[0];

    if (worksheet != null)
    {
        // fill some data ...
    }

    //return
    result = new MemoryStream(xlPackage.GetAsByteArray());
}

My first thought is that EPPLUS in not supported when publishing .NET CORE on Linux...? Or is there something we can do differently for it to work?


Solution

  • The problem was not with EPPLUS library, but the handling of file/path name for the Excel template. Windows Server is not case sensitive for file/path name, Linux on the other hand is.

    Once the folder name in our c# code was (case) matched with the actual folder name on Linux server, Excel export started working.