Connecting to PowerPivot with C#
Asked Answered
L

3

8

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?

Lemuel answered 23/11, 2011 at 12:57 Comment(0)
P
8

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:

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);

Primogenitor answered 28/8, 2012 at 17:59 Comment(1)
Do you pass the AMO connection string into a Server object? I get a "A connection cannot be made. Ensure that the server is running." error when I try to connect to my workbook.Scenic
L
2

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

Lifegiving answered 23/11, 2011 at 16:17 Comment(0)
A
1

Better yet, take a look at the C# source code of the Excel Refresh Service on codeplex - which demonstrates how to open and manipulate PowerPivot cubes that are embedded in Excel workbooks.

Be aware of what you're getting into... Excel automation has many quirks and tends to be unstable when used as a conduit into a PowerPivot cube.

Adoptive answered 25/10, 2012 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.