使用互操作的C#检测数据透视表在Microsoft Excel中 [英] Detect pivot table in Microsoft Excel using Interop c#
问题描述
我试图发现在Microsoft Excel单元格是否包含数据透视表通过使用Microsoft的Interop C#
我想要做的是遍历所有细胞如代码说明如下,然后如果单元格中包含数据透视表,我要存储单元格的行和列信息的整数值,例如:
INT rowCount等= xlRange.Rows.Count;
INT colCount = xlRange.Columns.Count;
Excel.Range电池= NULL;
为(INT iRow = 1; iRow< rowCount等; iRow ++)
{
为(INT ICOL = 1; ICOL< = colCount; ICOL ++)
{
/ *这行代码也许是错误的* /
细胞= xlRange.Cells [iRow,ICOL]作为Excel.Range;如果(/ *单元格中包含数据透视表* /)
{
INT rowLocation = iRow;
INT搭配= ICOL;
}
}
}
我试着看 MSDN 以及其他来源,但可以'不像是会找到检测的任何方法,如果单元格中包含数据透视表。
任何帮助表示赞赏,感谢。
下面是一些供参考的代码。
标识由数据透视表在表占据了整个范围和验证小区是否为范围的一部分。
<预类=郎-CS prettyprint-覆盖 >
私人Excel.Range IdentifyPivotRanges(Excel.Range xlRange)
{
Excel.Range pivotRanges = NULL;
Excel.PivotTables数据透视表= xlRange.Worksheet.PivotTables();
INT pivotCount = pivotTables.Count;
的for(int i = 1; I< = pivotCount;我++)
{
Excel.Range tmpRange = xlRange.Worksheet.PivotTables(我).TableRange2;
如果(pivotRanges == NULL)pivotRanges = tmpRange;
pivotRanges = this.Application.Union(pivotRanges,tmpRange);
}
返回pivotRanges;
}
私人无效CheckCellsForPivot(Excel.Range xlRange)
{
Excel.Range pivotRange = IdentifyPivotRanges(xlRange);
INT rowCount等= xlRange.Rows.Count;
INT colCount = xlRange.Columns.Count;
为(INT iRow = 1; iRow< = rowCount等; iRow ++)
{
为(INT ICOL = 1; ICOL< = colCount; ICOL ++)
{
细胞VAR = xlRange.Cells [iRow,ICOL]
如果(Application.Intersect(pivotRange,电池)!= NULL)
{
INT rowLocation = iRow;
INT搭配= ICOL;
}
}
}
}
I am trying to detect whether a cell in Microsoft excel contains a Pivot Table by using Microsoft Interop c#
What I want to do is loop through all of the cells as indicated in the code below, and then if the cell contains a pivot table, I want to store the row and column information of that cell in an integer value as such:
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;
}
}
}
I've tried looking at MSDN and other sources but can't seem to find any methods for detecting if a cell contains a pivot table.
Any help is appreciated, thanks.
Here is some code for reference. Identify the overall Range occupied by PivotTables in the sheet and validate whether the cell is a part of the Range.
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;
}
}
}
}
这篇关于使用互操作的C#检测数据透视表在Microsoft Excel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!