Search code examples
.net-coreasp.net-core-mvcepplus

EPPLUS Style.Fill.PatternType' threw an exception of type 'System.InvalidOperationException'


I'm working on a project in .NET Core. I need to mark any changes made to an object by highlighting it in yellow when it's changed. I can get it to work once but the second time around I get a 'System.InvalidOperationException' error. here is the code that runs into the error:

    public static void AddChangedColoring(string cellName, string updatedText) {
        FileInfo file = new FileInfo("tempSpreadsheet.xlsx");
        ExcelPackage package = new ExcelPackage(file);
        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
        Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#FFFF00");
        worksheet.Cells[cellName].Style.Fill.PatternType = ExcelFillStyle.Solid;
        worksheet.Cells[cellName].Style.Fill.BackgroundColor.SetColor(colFromHex);
        worksheet.Cells[cellName].Value = updatedText;
    }

I can't seem to find an answer anywhere, has anyone run into a situation similar to this? Can anyone explain why I may get this error and/or how to fix it?


Solution

  • I found the problem so I'll answer it in case anyone runs into a similar issue.

    The problem was in the cell name I was passing in. Its value wasn't being properly assigned so it was trying to make changes to cell 'A0' or 'B0' which doesn't exist.