Search code examples
c#asp.netexcelepplus

How to remove AutoFilter Using C# in EPPlus


I have tried the below C# CODING:

wsDt.Cells["A10:G10"].AutoFilter = false;

but the filter is not removed from my excel.

Any other way to remove it.

Thanks...


Solution

  • This seems to be an EPPlus bug and I don't think it has been resolved as of the latest release (4.04), at least I could figure out a solution. My workaround is to simply load the spreadsheet values a row at a time with a loop:

    int sheetRow = 3;
    for (int outer = 0; outer < outerSourceTable.Rows.Count; outer++)
    {
        var outerThingId = Convert.ToInt32(outerSourceTable.Rows[outer]["OuterThingId"]);
        var outerThingName = Convert.ToString(outerSourceTable.Rows[outer]["OuterThing"]);
        var innerThingsTable = _repository.GetInnerThings(outerThingId);
        if (innerThingsTable.Rows.Count > 0)
        {
            myWorksheet.Cells[sheetRow, 1].Value = outerThingName;
    
            // Load the data into the worksheet. We need to load a row at a time
            // to avoid the auto-filter bug
            for (int inner = 0; inner < innerThingsTable.Rows.Count; inner++)
            {
                var innerName = Convert.ToString(innerThingsTable.Rows[inner]["Name"]);
                var innerDescr = Convert.ToString(innerThingsTable.Rows[inner]["Description"]);
                myWorksheet.Cells[sheetRow, 2].Value = innerName;
                myWorksheet.Cells[sheetRow, 3].Value = innerDescr;
                sheetRow++;
            }
            sheetRow++;
        }
    }