Excel.PivotField.DataRange.Cells.Group 不起作用。为什么?

Excel.PivotField.DataRange.Cells.Group does not work. Why?

以下代码无法构建。

            var Date = (Excel.PivotField)pivotTable.PivotFields("Date");
            Date.Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
            Date.Position = 1;
            Date.DataRange.Cells[1].Group(true, true, Type.Missing, GroupParam);

Group 被标记为导致错误。错误信息是'object' does not contain a definition for 'Group' and no extension method 'Group' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

我该如何解决这个错误?

我将 VS 2012 与 VSTO 和加载项 express 一起使用

如果遇到编译问题,您可能应该声明中间变量。从测试中可以明显看出 Group 方法存在并且您的代码应该可以访问该方法。给编译器一些帮助并声明变量:

这种哲学的极端版本​​对我来说效果很好 Group:

private void button2_Click(object sender, EventArgs e)
{

    Excel.Worksheet sht = app.ActiveSheet;
    Excel.PivotTable pt = sht.PivotTables(1);
    Excel.PivotField pf = pt.PivotFields("DATE");

    Excel.Range rng = pf.DataRange;

    Excel.Range cell = rng.Cells[1];
    cell.Group(true, true, Type.Missing, new bool[] { false, false, false, false, true, true, false });
}
Excel.Range rng = pf.DataRange;

    Excel.Range cell = rng.Cells[1];
    cell.Group(true, true, Type.Missing, new bool[] { false, false, false, true, false, true, true });

如上所述,bool 数组中传递的参数数量为true 和false。该值设置为 true 或 false,具体取决于您想要的过滤器,例如月份、年份等。

见图。参数顺序与传值相同