Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to PowerPivot with C#

Is it possible to connect to a PowerPivot model in an Excel .xlsx file? (Not hosted on a SharePoint site... just the local file).

It must be, since Tableau can do it.

Anyone have any clues?

like image 739
WOPR Avatar asked Nov 23 '11 12:11

WOPR


People also ask

What language does Power Pivot use?

Power Pivot primarily uses Data Analysis Expressions (DAX) as its expression language, although the model can be queried via Multidimensional Expressions (MDX) in a row set expression.

How do I get data from Power Pivot to Excel?

First, import your dataset into your Power Pivot workbook. Next create a Power Pivot table. Then, on the Power Pivot tab, click the arrow below PivotTable and select PivotChart. Select “Existing Worksheet” and click “OK.” Excel will add an empty PivotChart to the same worksheet.


2 Answers

Short answer:

  • Yes

Long answer:

  • Updated versions of either Excel or PP (maybe even Office service packs?) could break your code
  • We were successful in connecting to the PP model using AMO (to add tables to the data source view, add dimensions, add measures, and refresh the model from external DBs). We found Tabular AMO library on codeplex (https://tabularamo2012.codeplex.com/) to be VERY helpful.
  • We were successful in connecting to the PP model using ADO (but not ADOMD) to query the model (for instance, query the value of a measure).

References/Credits:

  • https://gobansaor.wordpress.com
  • http://sqlblog.com/blogs/default.aspx
  • http://powerpivotgeek.com/2009/11/11/a-peek-inside-the-client-architecture/#comments

Details:

  • Like @gobansaor, we found that starting with a workbook which already had a connection to the PP cache was helpful (necessary?). Before connecting to the PP cache via AMO for instance, we make sure the connection is alive:

    ThisWorkbook.Connections["PowerPivot Data"].Reconnect()

    or

    ThisWorkbook.Connections["PowerPivot Data"].Refresh()

  • The connection string template we used for AMO was: Provider=MSOLAP;Data Source=$Embedded$;Locale Identifier=1033;Location={0};SQLQueryMode=DataKeys and we filled that in with ThisWorkbook.FullName

  • Following @gobansaor, we connected to the cube via ADO using:

    ADODB.Recordset recordSet = new ADODB.Recordset();

    recordSet.Open("SELECT [Measures].[Min of Field1] ON COLUMNS FROM [Model]", ThisWorkbook.Connections["PowerPivot Data"].OLEDBConnection.ADOConnection);

like image 140
gap Avatar answered Sep 30 '22 09:09

gap


You can build a VSTO addin.

Here's a site that helps explain working with PowerPivot and VSTO.

http://blogs.msdn.com/b/analysisservices/archive/2011/08/04/how-to-build-a-vsto-based-powerpivot-workbook.aspx

like image 33
Justin Self Avatar answered Sep 30 '22 10:09

Justin Self