cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
paritysupport New Member
New Member

Update many excel files Power Query Data sources

Hello,

 

We have created a script using standard pivots tables and excel to update many excel files at once with a new DB connection with the script below (maybe this will be helpful to some!). This lets us update a folder of pre-built excel reports at once with a specified Server and DB name, keeping their views they are tied to.

 

We are updating our standard pivots to take advantage of Power Query & using dashboards. Our goal is to convert this script below to work with updating the power query datasources, rather than the default excel connections in the same manner. (Drop excel files with power queries into a folder, define the new datasource settings (Server Name & DB) , and update all files)  The original person who wrote this script has moved on several years ago - so any help is appreciated if you can point us in the right direction! 

 

 

public void UpdateConnectionStrings(List<FileInfo> pExcelFiles, string pOldServerName, string pOldDatabaseName, string pNewServerName, string pNewDatabaseName)

    {

      Microsoft.Office.Interop.Excel.Application application = null;

      try

      {

        // Create a new application

        application = new Microsoft.Office.Interop.Excel.Application();

        foreach (FileInfo excelFile in pExcelFiles)

        {

          // Open the file

          application.Workbooks.Open(excelFile.FullName);

          // Get the query tables from the worksheets

          var sheets = application.Sheets.OfType<Worksheet>();

          var queryTables = sheets.SelectMany(s => GetQueryTables(s));

          // Change the connection string for any query tables

          foreach (var queryTable in queryTables)

          {

            queryTable.Connection = queryTable.Connection.Replace(pOldServerName, pNewServerName);

            queryTable.Connection = queryTable.Connection.Replace(pOldDatabaseName, pNewDatabaseName);

          }

          // Get the pivot table data from the workbooks

          var workbooks = application.Workbooks.Cast<Workbook>();

          var pivotCaches = workbooks.SelectMany(w => GetPivotCaches(w));

          // Change the connection string for any pivot tables

          foreach (var pivotCache in pivotCaches)

          {

            pivotCache.Connection = pivotCache.Connection.Replace(pOldServerName, pNewServerName);

            pivotCache.Connection = pivotCache.Connection.Replace(pOldDatabaseName, pNewDatabaseName);

          }

          foreach (var workbook in workbooks)

          {

            workbook.Save();

            workbook.Close();

          }

        }

      }

      catch (Exception e)

      {

        throw new Exception(e.Message);

      }

      finally

      {

        // Make sure we quit the application

        if (application != null)

        {

          application.Quit();

        }

      }

    }

Helpful resources

Announcements
Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Top Kudoed Authors
Users Online
Currently online: 65 members 1,365 guests
Please welcome our newest community members: