简体   繁体   English

如何确定PivotTable.DataBodyRange中的单元格是否为“总计”

[英]How to determine if a cell in PivotTable.DataBodyRange is a “total”

I create a pivot table in code (C#) with grand totals for columns. 我用代码(C#)创建一个数据透视表,其中包含列的总计。 How can I determine, if a cell in the PivotTable.DataBodyRange is a total? 如何确定PivotTable.DataBodyRange的单元格是否为总数? I loop through the cells like so: 我像这样遍历单元格:

foreach (Excel.Range cell in pvtTable.DataBodyRange) { //I need to determine if "cell" is a total or not. }

See image. 见图片。 The cells that are totals are highlighted in yellow. 总计的单元格以黄色突出显示。

在此处输入图片说明

You need this: 你需要这个:

  • Loop over the rows by looping over PivotTable.PivotRowAxis.PivotLines 通过遍历PivotTable.PivotRowAxis.PivotLines遍历行
  • Each PivotLine.Position is equivalent the row number within the PivotTable.DataBodyRange (not the Excel row number), starting with 1 每个PivotLine.Position是等效的行数PivotTable.DataBodyRange (未Excel的行编号),从1开始
  • If PivotLine.LineType = xlPivotLineSubtotal , then you are in a desired row 如果PivotLine.LineType = xlPivotLineSubtotal ,那么您在所需的行中

The result might be something like this: 结果可能是这样的:

foreach (Excel.PivotLine pl in pvtTable.PivotRowAxis.PivotLines)
{
    if (pl.LineType == Excel.XlPivotLineType.xlPivotLineSubtotal)
    {
        foreach (Excel.Range cell in pvtTable.DataBodyRange.Rows[pl.Position].Cells){
            System.Diagnostics.Debug.WriteLine(cell);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM