简体   繁体   中英

Turn Off Filters

So, I'm doing some searching in an Excel document, but it is very common for others to turn on filters and leave them on. When those filters are on, those cells are not included in the worksheet's Cells range.

Is there a way to turn off these custom filters so that I can still get to all of the cells in the sheet?

This is the way I use to find the method

Microsoft.Office.Interop.Excel.Range find = sheet.Cells.Find(tapeID, Type.Missing,
Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlPart, 
Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing); 

When the filters are on, I get a null object returned and can't do anything but with the filters off, I get what I need.

Any hints on turning the filters off?

I would test first to see if a filter has been applied and then deactivate it if it has:

if (xlSheet.AutoFilter != null)
{
    xlSheet.AutoFilterMode = false;
}

That should remove any filtering that has been applied and remove the filter arrow buttons.

You can disable all filters by calling the AutoFilter method on the range twice with no parameters.

sheet.Cells.AutoFilter();
sheet.Cells.AutoFilter();

I'm not very Interop savvy, but you may need to pass 5 Type.Missing or Missing.Value as parameters.

The first call will turn AutoFilter off if it's on, and the second will turn it on if it's off and vice versa. But in either case there will no longer be hidden cells due to filtering.

I used the following code because xlSheet.AutoFilterMode = false throws as COMException for me even though xlSheet.AutoFilterMode is true .

if (xlSheet.AutoFilter != null && xlSheet.AutoFilterMode == true)
{
    xlSheet.AutoFilter.ShowAllData();
}

As mentioned by Sid Holland , this clears all filters while also retaining the filter arrows.

If you want to turn off all filters on the sheet, including tables, here is some code. It seems there is no off the shelf method to clear all filters, including tables, and worse when you try to clear filters and there's a table on the sheet, we've noticed sometimes exceptions are thrown. So what we do is try both ways- for tables and regular sheets, and swallow & log any exceptions we find.

Note: I'm not including logging dependencies here, so I've commented that out. For the record, we're using log4net.

using Microsoft.Office.Interop.Excel;

class WorksheetDecoratorImpl
{
    public Worksheet Worksheet { get; private set; }

    public string Name => Worksheet.Name;

    public void TryClearAllFilters()
    {
        try
        {
            if (Worksheet.AutoFilter != null)
            {
                Worksheet.AutoFilterMode = false;
            }
        }
        catch(Exception ex)
        {
            //Log.Error(string.Format("Clear filters encountered an issue. Sheet: {0}", Name));
        }
        try
        {
            ListObjects listObjects = Worksheet.ListObjects;                
            foreach(ListObject listObject in listObjects)
            {
                listObject.AutoFilter.ShowAllData();
            }
        }
        catch (Exception ex)
        {
            //Log.Error(string.Format("Clear table filters encountered an issue. Sheet: {0}", Name));
        }
    }
}

References

Clear filter from Table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM