如何在C#中以特定的自定义顺序列出方向为xlColumnField的枢轴字段

时间:2019-06-14 18:13:21

标签: c# excel pivot-table

我将重点放在运动领域。假设有4种运动(网球,棒球,垒球,足球)。创建数据透视表后,我希望以自定义顺序显示列标签。我该如何在C#中做到这一点。

enter image description here

1 个答案:

答案 0 :(得分:0)

                    Excel.PivotField pf = (Excel.PivotField)pvtTable.PivotFields("Sport");
                //Desired list order.
                List<string> oListOrder = new List<string>() { "Football", "Tennis", "Baseball", "Softball" };  
                foreach (Excel.PivotItem pi in pf.PivotItems()) {
                    //Get index in desired list order.
                    int idx = oColumnSort.IndexOf(pi.Name); 
                    //If PivotItem in ordered list set PivotItem position to that of list. 
                    //Add 1 bc Position & List have different starting base.
                    if (idx >= 0) pi.Position = idx + 1;
                }