【发布时间】:2012-09-30 00:35:03
【问题描述】:
在这里,我正处于我的智慧边缘,花了一整天的时间来尝试做一些不应该如此复杂的事情。
我有一个从 Sybase 查询返回的 Recordset。此记录集用于在 Excel 中构建数据透视表。到目前为止,一切都很好。我想更改数据透视表中的一个值,为此我使用新值来更新记录集中的某些记录。我可以毫无问题地在 RS 中进行更新,并且下次迭代时将值保存在 RS 中。
问题是这些值没有反映在数据透视表中。我试过了:
-
pivotTable.Refresh();- COMException:数据透视表类的 RefreshTable 方法失败
-
pivotTable.PivotCache().Refresh();- ComException:HRESULT 异常:0x800A03EC
-
pivotTable.Update();- 没有例外,但更改不会反映在数据透视表中
我也尝试克隆记录集并从中创建一个全新的数据透视表,但尽管Recordset 中有数据,PivotCache.RecordCount 是 0
代码:
var app = ExcelAppHelper.GetExcelApp();
if (app.ActiveCell == null || app.ActiveCell.PivotTable == null)
return;
PivotTable pivotTable = app.ActiveCell.PivotTable;
var rs = (Recordset)pivotTable.PivotCache().Recordset;
rs.MoveFirst();
s_lastSelectedPivotTree = new PivotFilterTree();
RecalculateSelectedValues(vmMain);
while (!rs.EOF)
{
if (s_lastSelectedPivotTree.Contains(rs.Fields))
{
foreach (var dataFieldName in s_lastSelectedDataFields)
{
// update the values in the RS
rs.Fields[dataFieldName].Value = newValue;
}
// commit the modifications into the RS
rs.Update(Type.Missing, Type.Missing);
}
rs.MoveNext();
}
rs.MoveFirst();
// here is the magic line that will show me the updated pivot table
pivotTable.Update();
有人知道怎么做吗?修改记录集,然后“刷新”数据透视表以根据记录集重新计算数据透视表。
谢谢 肖恩
【问题讨论】:
标签: excel vsto refresh pivot-table recordset