cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
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
New Kudos Received Badges Coming

New Kudos Received Badges Coming

Kudos to you if you earned one of these! Check your inbox for a notification.

Microsoft Implementation for Communities Wins Award

Microsoft Implementation for Communities Wins Award

Learn about the award-winning innovation that was implemented across Microsoft’s Business Applications Communities.

Power Platform World Tour

Power Platform World Tour

Find out where you can attend!

Top Solution Authors
Top Kudoed Authors (Last 30 Days)
Users online (4,317)