Search code examples
c#epplus

Get error cells by GetCellValue<eErrorType> or a with a different approach


formulas in excel can return different errors e.g. #div/0 But how to check a cell

package.Workbook.Worksheets["a"].Cells["g2"].GetCellValue<eErrorType>(); will return the error type if an error exists but will crash if the formula of the cell will not produce an error. As far as I can see, the enum of eErrorType does not contain a member like NoError :-(

I would like to use something like that:

var badCells = package.Workbook.Worksheets["a"].Cells.All(f => f.GetCellValue<eErrorType>()!???

Any other approach welcome

tx Perry


Solution

  • Seems like you need to test by type like this:

    var workbook = package.Workbook;
    var worksheet = workbook.Worksheets["Sheet1"];
    foreach (var cell in worksheet.Cells)
    {
        var x = cell.GetCellValue<object>();
        switch (x)
        {
            case double d:
                Console.WriteLine($"{cell.Address} is double: {d}");
                break;
            case ExcelErrorValue error:
                Console.WriteLine($"{cell.Address} with formula '{cell.Formula}' is error: {error.Type}");
                break;
        }
    }
    

    Assuming you have a sheet like: enter image description here

    Which give this as the output:

    A1 is double: 1
    B1 is double: 0
    C1 with formula 'A1/B1' is error: Div0
    

    RESPONSE TO COMMENTS:

    A foreach would probably be more efficient since you can perform any other needed tasks inside of it but this is how to do it via LINQ:

    var errorCells = worksheet
        .Cells
        .Where(c => c.GetCellValue<object>() is ExcelErrorValue)
        .ToList();
    
    Console.WriteLine($"Number of error cells: {errorCells.Count}"); // Prints "1"