Is there a “sister” function to “AutoFilter” in Excel Interop that works for columns instead of rows?

前端 未结 1 381
太阳男子
太阳男子 2021-01-27 23:32

The \"AutoFilter\" function in Excel Interop allows you to filter what data in the rows beneath a column display.

I need a similar function to allow the user to select w

相关标签:
1条回答
  • 2021-01-28 00:06

    I don't think there is such functionality, but similar can be achieved using PivotTables.

    I still have work to do on getting it all to look decent, but I did find out how to create a column filter, based on an answer here.

    With the following code:

    var pch = _xlBook.PivotCaches();
    // TODO: Make range dynamic
    Range sourceData = _xlBook.Worksheets["PivotData"].Range["A1:G318"];
    
    PivotCache pc = pch.Create(XlPivotTableSourceType.xlDatabase, sourceData);
    PivotTable pvt = pc.CreatePivotTable(_xlPivotTableSheet.Range["A8"], "PivotTable");
    
    pvt.PivotFields("Description").Orientation = XlPivotFieldOrientation.xlRowField;
    pvt.PivotFields("MonthYr").Orientation = XlPivotFieldOrientation.xlColumnField;
    

    ...I can take the source data from one sheet ("PivotData") and slap it on another sheet (_xlPivotTableSheet). The part that gives me the "filter a set of columns" functionality is the last line above. That leaves me with the following:

    Though you cannot see it, there is a column "201509" as well as the "201510", and they can be filtered to display or not, individually or collectively.

    Now to get the rest of the data to display as it should...

    0 讨论(0)
提交回复
热议问题