简体   繁体   中英

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. 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.

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. 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'. 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.

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. 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 ?

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
    }
}

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