简体   繁体   English

使用vsto c#跟踪Excel中的数据透视表过滤器

[英]Tracking pivot table Filters in Excel using vsto c#

Could anybody suggest how to get the details of Filters that are applied on an Excel Pivot Table. 任何人都可以建议如何获取在Excel数据透视表上应用的筛选器的详细信息。 Also from a set of Pivot Tables from current sheet, how to identify those pivot tables in which the filters are applied. 同样从当前工作表的一组数据透视表中,如何识别那些在其中应用了过滤器的数据透视表。 Thanks in Advance.. 提前致谢..

I can only comment on how to do this using VBA, which may be of help to you as a starting point. 我只能评论如何使用VBA进行此操作,这可能对您有所帮助。

Keeping track of PivotTable Filters is devilishly hard. 跟踪数据透视表筛选器异常困难。 Even just working out which PivotField changed is challenging, because the only event that gets generated when they change is a PivotTable_Refresh event, which doesn't even tell you which particular field just changed. 即使只是确定哪个PivotField发生了更改也是具有挑战性的,因为更改时唯一会生成的事件是PivotTable_Refresh事件,该事件甚至无法告诉您刚刚更改了哪个特定字段。 In fact, that event also gets generated for a whole raft of other things nothing to do with filtering, as outlined at my post at http://dailydoseofexcel.com/archives/2014/07/08/what-caused-that-pivottableupdate-part-two/ so if you try to use that event to do something related to filters then your code runs unnecessarily a lot of the time, unless you check the UNDO stack and only run it when the stack says 'Filter'. 实际上,该事件也会因其他大量与过滤无关的事情而生成,如我在http://dailydoseofexcel.com/archives/2014/07/08/what-c​​aused-that-pivottableupdate中的帖子所述-part-two /,因此,如果您尝试使用该事件来执行与过滤器相关的操作,则您的代码会不必要地大量运行,除非您检查UNDO堆栈并仅在堆栈显示“ Filter”时才运行它。 I have a very long code listing at http://dailydoseofexcel.com/archives/2014/07/10/what-caused-that-pivottableupdate-episode-iv/ that uses a number of approaches to identify which PivotField just changed. 我在http://dailydoseofexcel.com/archives/2014/07/10/what-c​​aused-that-pivottableupdate-episode-iv/上列出了很长的代码,该代码使用多种方法来标识刚刚更改的PivotField。

About the easiest way to keep track of filters is to create a whole bunch of copies of each PivotTable - one for each field - that each contain nothing but that field as a RowField. 跟踪筛选器的最简单方法是为每个数据透视表创建一堆副本(每个字段一个副本),每个副本不包含任何内容,但该字段作为RowField。 Then connect those 'slave' PivotTables to the master via a slicer, so any changes to filters in the master are reflected in the slave. 然后通过切片器将那些“从属”数据透视表连接到主服务器,以便对主服务器中的筛选器进行的任何更改都将反映在从属服务器上。 This shows you the visible items for each field (ie which items are NOT filtered out). 这将显示每个字段的可见项目(即未过滤掉哪些项目)。 Then you can monitor those slave PivotTables for changes. 然后,您可以监视这些从数据透视表的更改。 Ugly, but it works. 丑陋,但是行得通。

Have you tried Excel.PivotTable.ActiveFilters ? 您是否尝试过Excel.PivotTable.ActiveFilters

And you can iterate pivot table on a certain sheet like this: 您可以像这样在某个工作表上迭代数据透视表:

Excel.PivotTables pivotTables = worksheet.PivotTables(Type.Missing);

foreach (Excel.PivotTable p in pivotTables){
    if(p.ActiveFilters.Count > 1){
         //your code
    }
}

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

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