Search code examples
c#epplus

Win Forms Compile Error Of cannot explicitly call operator or accessor


I am using the EPPlus library for my C# Win forms project but am getting this error:

'ExcelPackage.Workbook.get': cannot explicitly call operator or accessor

On this line of code: (the get_workbook & get_name is what is throwing the error)

    str = str.Trim(new char[] { '\'' }).Replace("\\", "").Replace("/", "").Replace("*", "").Replace("[", "").Replace("]", "").Replace(":", "").Replace("?", "");
using (ExcelPackage excelPackage = new ExcelPackage(new FileInfo(saveFile)))
{
    if (excelPackage.get_Workbook().get_Worksheets().Any<ExcelWorksheet>((ExcelWorksheet w) => w.get_Name() == str))
    {
        excelPackage.get_Workbook().get_Worksheets().Delete(str);
    }
}

What am I calling incorrectly and how should I update?


Solution

  • Don't access properties that way. In EPPlus, you can just reference the Workbook and Worksheets properties. Also, I think you'll have a problem trying to delete a WorkSheet using a string.

    Assuming you could at most have one worksheet that matched the name exactly, try this instead:

    var matchingWorksheet = excelPackage.Workbook.Worksheets.SingleOrDefault(w => w.Name == str);
    
    if (matchingWorksheet != null)
    {
        excelPackage.Workbook.Worksheets.Delete(matchingWorksheet);
    }