使用Interop c检测Microsoft Excel中的数据透视表#

我正在尝试使用Microsoft Interop c#检测Microsoft Excel中的某个单元是否包含数据透视表

我想要做的是循环所有的单元格,如下面的代码所示,然后如果单元格包含一个数据透视表,我想存储该单元格的行和列信息的整数值,如下所示:

int rowCount = xlRange.Rows.Count; int colCount = xlRange.Columns.Count; Excel.Range cell = null; for (int iRow = 1; iRow < rowCount; iRow++) { for (int iCol = 1; iCol <= colCount; iCol++) { /* This line of code is probably wrong */ cell = xlRange.Cells[iRow, iCol] as Excel.Range; if(/*Cell contains PivotTable*/) { int rowLocation = iRow; int colLocation = iCol; } } } 

我试着看MSDN和其他来源,但似乎无法find任何方法来检测单元格是否包含数据透视表。

任何帮助表示赞赏,谢谢。

这里有一些代码供参考。 确定工作表中数据透视表占用的整个范围,并validation单元格是否为范围的一部分。

 private Excel.Range IdentifyPivotRanges(Excel.Range xlRange) { Excel.Range pivotRanges = null; Excel.PivotTables pivotTables = xlRange.Worksheet.PivotTables(); int pivotCount = pivotTables.Count; for (int i = 1; i <= pivotCount; i++) { Excel.Range tmpRange = xlRange.Worksheet.PivotTables(i).TableRange2; if (pivotRanges == null) pivotRanges = tmpRange; pivotRanges = this.Application.Union(pivotRanges, tmpRange); } return pivotRanges; } private void CheckCellsForPivot(Excel.Range xlRange) { Excel.Range pivotRange = IdentifyPivotRanges(xlRange); int rowCount = xlRange.Rows.Count; int colCount = xlRange.Columns.Count; for (int iRow = 1; iRow <= rowCount; iRow++) { for (int iCol = 1; iCol <= colCount; iCol++) { var cell = xlRange.Cells[iRow, iCol]; if (Application.Intersect(pivotRange, cell) != null) { int rowLocation = iRow; int colLocation = iCol; } } } } 

看看这里有几个关于通过数据透视表列表循环的例子

 pivotSheet.Activate(); Microsoft.Office.Interop.Excel.PivotTables pivotTables = (Microsoft.Office.Interop.Excel.PivotTables)pivotSheet.PivotTables(missing); int pivotTablesCount = pivotTables.Count; 

刷新C#中的Excel数据透视表