【问题标题】:Detect pivot table in Microsoft Excel using Interop c#使用 Interop c# 在 Microsoft Excel 中检测数据透视表
【发布时间】:2016-02-25 09:51:08
【问题描述】:

我正在尝试使用 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 和其他来源,但似乎找不到任何方法来检测单元格是否包含数据透视表。

感谢您的帮助。

【问题讨论】:

    标签: c# excel interop


    【解决方案1】:

    这里有一些代码供参考。 识别工作表中数据透视表占用的整个 Range,并验证单元格是否属于 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;
                }
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答,稍微调整一下这个解决方案就解决了我的问题
    • 这太好了,感谢分享,我和 Trebblez 一样无法在文档或其他任何地方找到明确的答案。
    【解决方案2】:

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

    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#

    【讨论】:

    【解决方案3】:

    扩展 CS 的答案:我使用了 CS 的答案,但做了一些修改,以便您只需要工作簿的路径。不需要范围,只需传递工作簿中的工作表。

    #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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-30
      • 2023-03-10
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多