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

下面的代码不会生成。

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和加载项快车

如果您遇到编译问题,您应该声明中间variables。 从testing中可以看出, Group方法存在,应该可以通过代码访问。 给编译器一些帮助,并声明variables:

这个哲学的极端版本​​适用于我使用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,取决于您想要的月份,年份等的filter。

看图片。 参数顺序与传递值相同 在这里输入图像说明