简体   繁体   English

关闭过滤器

[英]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. 所以,我正在Excel文档中进行一些搜索,但是其他人打开过滤器并让它们保持打开是很常见的。 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. 当过滤器打开时,我得到一个返回的null对象,除了关闭过滤器之外无法做任何事情,我得到了我需要的东西。

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. 您可以通过在没有参数的情况下调用范围两次的AutoFilter方法来禁用所有过滤器。

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. 我不是非常精通Interop,但你可能需要传递5个Type.MissingMissing.Value作为参数。

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. 第一个调用将在AutoFilter打开时关闭,第二个调用将在关闭时将其打开,反之亦然。 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 . 我使用以下代码,因为即使xlSheet.AutoFilterModetrue xlSheet.AutoFilterMode = false也会为我抛出COMException。

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. Sid Holland所述 ,这会清除所有过滤器,同时保留过滤器箭头。

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. 为了记录,我们正在使用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 从表中清除过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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