Excel VSTO:创建OLAP数据透视表时出现异常-VisibleItemsList

时间:2018-12-23 15:48:05

标签: c# excel vsto olap

我想在VSTO C#插件中创建连接到SSAS数据库的Excel数据透视表。我可以设置字段和过滤器,但不能设置行/列字段的可见项选择。

这是我现在拥有的代码,最后一行抛出错误:

  

System.Runtime.InteropServices.COMException:'来自HRESULT的异常:0x800A03EC

PivotCache pivotCache =
        worksheet.Application.ActiveWorkbook.PivotCaches().Add(XlPivotTableSourceType.xlExternal, Missing.Value);
pivotCache.Connection = "OLEDB;Provider=MSOLAP.8;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Sample_SSAS;Data Source=localhost;MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error;Update Isolation Level=2";
pivotCache.MaintainConnection = true;
pivotCache.CommandText = "SampleCube";
pivotCache.CommandType = XlCmdType.xlCmdCube;
pivotCache.MissingItemsLimit = XlPivotTableMissingItems.xlMissingItemsNone;


PivotTables pivotTables = (PivotTables)worksheet.PivotTables(Missing.Value);
var cell = worksheet.Cells[1, 1];
PivotTable pivotTable = pivotTables.Add(pivotCache, cell, "PivotTable1",
            Missing.Value, Missing.Value);

CubeField cubeField = pivotTable.CubeFields["[Date].[Year - Quarter - Month - Date]"];
cubeField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;

var pivotField = pivotTable.PivotFields("[Date].[Year - Quarter - Month - Date].[Year]");


var visibleItems = new string[]
            {
    "[Date].[Year - Quarter - Month - Date].[Year].&[2015]",
    "[Date].[Year - Quarter - Month - Date].[Year].&[2016]",
    "[Date].[Year - Quarter - Month - Date].[Year].&[2017]",
    "[Date].[Year - Quarter - Month - Date].[Year].&[2018]"
            };

// throws System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'
pivotField.VisibleItemsList = visibleItems;

之所以这样做,是因为我记录了一个创建数据透视表的宏,并试图复制VBA的这一段:

With ActiveSheet.PivotTables("PivotTable2").CubeFields( _
    "[Date].[Year - Quarter - Month - Date]")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").PivotFields( _
    "[Date].[Year - Quarter - Month - Date].[Year]").VisibleItemsList = Array( _
    "[Date].[Year - Quarter - Month - Date].[Year].&[2008]", _
    "[Date].[Year - Quarter - Month - Date].[Year].&[2009]", _
    "[Date].[Year - Quarter - Month - Date].[Year].&[2010]")

不过,我显然缺少了一些东西。

0 个答案:

没有答案