用C#连接PowerPivot
是否可以连接到Excel .xlsx文件中的PowerPivot模型? (不在SharePoint网站上托pipe…只是本地文件)。
肯定是的,因为Tableau可以做到这一点。
任何人有任何线索?
简短的回答:
- 是
很长的回答:
- 更新版本的Excel或PP(甚至可能是Office Service Pack?)都可能会破坏您的代码
- 我们成功地使用AMO连接到PP模型(将数据添加到数据源视图,添加维度,添加度量以及从外部数据库刷新模型)。 我们在codeplex上findTabular AMO库(https://tabularamo2012.codeplex.com/)非常有帮助。
- 我们成功地使用ADO(而不是ADOMD)连接到PP模型来查询模型(例如,查询度量的值)。
参考文献/学分:
- http://blog.gobansaor.com/
- http://sqlblog.com/blogs/default.aspx
- http://powerpivotgeek.com/2009/11/11/a-peek-inside-the-client-architecture/#comments
细节:
-
像@ gobansaor一样,我们发现从一个已经与PPcaching连接的工作簿开始是有帮助的(必要的)。 在通过AMO连接到PPcaching之前,我们确保连接处于活动状态:
ThisWorkbook.Connections["PowerPivot Data"].Reconnect()
要么
ThisWorkbook.Connections["PowerPivot Data"].Refresh()
-
我们用于AMO的连接string模板是:
Provider=MSOLAP;Data Source=$Embedded$;Locale Identifier=1033;Location={0};SQLQueryMode=DataKeys
,我们使用ThisWorkbook.FullName
-
在@gobansaor之后,我们使用ADO连接到多维数据集:
ADODB.Recordset recordSet = new ADODB.Recordset();
recordSet.Open("SELECT [Measures].[Min of Field1] ON COLUMNS FROM [Model]", ThisWorkbook.Connections["PowerPivot Data"].OLEDBConnection.ADOConnection);
你可以构build一个VSTO插件。
这是一个帮助解释PowerPivot和VSTO的网站。
更好的方法是查看codeplex上的Excel刷新服务的C#源代码,演示如何打开和操作embedded在Excel工作簿中的PowerPivot多维数据集。
请注意您正在进入的内容… Excel自动化有许多怪癖,并且在用作PowerPivot多维数据集的pipe道时往往不稳定。