[英]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: 你需要这个:
PivotTable.PivotRowAxis.PivotLines
PivotTable.PivotRowAxis.PivotLines
遍历行 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开始 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.