从C#刷新的Excel透视表 [英] Refreshing an Excel Pivot table from C#

查看:702
本文介绍了从C#刷新的Excel透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图刷新数据透视表在Excel工作表,并得到以下异常:在数据透视表

I am trying to refresh a pivot table in an Excel sheet and get the following exception:

Item method in the PivotTables class failed

继承人的代码:

pivotSheet.Activate();
Microsoft.Office.Interop.Excel.PivotTables pivotTables = 
        (Microsoft.Office.Interop.Excel.PivotTables)pivotSheet.PivotTables(missing);
int pivotTablesCount = pivotTables.Count;  
    if (pivotTablesCount > 0)
    {
        for (int i = 0; i <= pivotTablesCount; i++)
        {
            pivotTables.Item(i).RefreshTable(); //The Item method throws an exception
        }
    }



你知道吗?

Any idea?

推荐答案

假设索引从零开始,你会充斥着你的循环集合。

Assuming the indexing starts at zero you will overrun the collection with your loop.

尝试:

for (int i = 0; i < pivotTablesCount; i++)

如果不工作的Excel可能从1开始索引不为0。

If that doesn't work Excel probably starts indexing at 1 not at 0.

尝试:

for (int i = 1; i <= pivotTablesCount; i++)

这篇关于从C#刷新的Excel透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆