简体   繁体   English

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

[英]Detect pivot table in Microsoft Excel using Interop c#

I am trying to detect whether a cell in Microsoft excel contains a Pivot Table by using Microsoft Interop c# 我试图通过使用Microsoft Interop c#来检测Microsoft Excel中的单元格是否包含数据透视表

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. 我已经尝试过查看MSDN和其他来源,但似乎无法找到任何方法来检测单元格是否包含数据透视表。

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;
            }
        }
    }
}

look here is got a couple of examples about looping through Pivot tables list 看看这里有几个关于循环数据透视表列表的例子

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

Refreshing an Excel Pivot table from C# 从C#刷新Excel数据透视表

Expanding on CS's Answer: I used CS's answer but with some modification so that all you need is the path to your WorkBook. 扩展CS的答案:我使用了CS的答案,但进行了一些修改,以便您只需要通往WorkBook的路径。 No range needed, just pass the worksheets within the workbook. 无需范围,只需传递工作簿中的工作表即可。

#region PivotTables

public static List<DataTable> PivotCollection(string filePath)
{
    List<DataTable> pivotCollection = new List<DataTable>();

    //Get Workbook
    xl.Application app = new xl.Application();
    xl.Workbook wb = null;
    wb = app.Workbooks.Open(filePath);

    //Loop through each worksheet and find pivot tables to add to collection
    foreach (xl.Worksheet ws in wb.Worksheets)
    {
        //Get range of pivot table
        string wsName = ws.Name.ToString();
        xl.Range pivotRange = GetPivotRange(ws);

        //Check if there is any table to add
        if (pivotRange == null) continue;
        DataTable pivotTable = DataTables.FromRange(pivotRange);

        //Get info from range and add to pivot
        pivotCollection.Add(pivotTable);

    }

    return pivotCollection;

}

//Find Pivot table in worksheet
//CS's Code with slight modification, use worksheet instead of range
public static xl.Range GetPivotRange(xl.Worksheet xlworkSheet)
{
    xl.Range pivotRanges = null;
    xl.PivotTables pivotTables = xlworkSheet.PivotTables();
    int pivotCount = pivotTables.Count;
    for (int i = 1; i <= pivotCount; i++)
    {
        xl.Range tmpRange = xlworkSheet.PivotTables(i).TableRange2;
        if (pivotRanges == null) pivotRanges = tmpRange;
        pivotRanges = tmpRange;
    }
    return pivotRanges;
}
#endregion

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

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