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
New Topics Started Badges Coming

New Topics Started Badges Coming

We're releasing new versions of the badge that everyone's talking about. ;) Check your inbox for notifications.

MBAS 2020

Save the new date (and location)!

Our business applications community is growing—so we needed a different venue, resulting in a new date and location. See you there!

Difinity Conference

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors (Last 30 Days)