Are you loading data from an Excel workbook with external queries using SSIS? Do you want to make sure those queries are refreshed before the load takes place? Have I got the post for you! I recently had to do this myself, and after some googling with Bing, I found an MSDN forum post that describes how to refresh all of the queries within a workbook using an SSIS Script Task. I’ve adapted the code from the post, and you can find it below.
For the code to compile, you’ll need to add a reference to Microsoft.Office.Interop.Excel within your script task’s project.
Next, select the reference under the project explorer, and open the properties window. You’ll need to disable the Embed Interop Types option. (This assumes you’re using SQL 2012, which supports .NET 4 … if you are using 2008 with .NET 2/3.5, you won’t have to do this).
Include the following code in your script task. It iterates over all of the Excel connection managers within your package, loads the workbook, and calls RefreshAll() to refresh each of the queries in the workbook. The task then sleeps for 20 seconds (to give time for the query refresh to complete) before exiting.
public void Main()
const string DataSourceMarker = "data source=";
// refresh each Excel connection manager
foreach (var conn in Dts.Connections)
if (conn.CreationName == "EXCEL")
string filepath = null;
var parts = conn.ConnectionString.Split(';');
foreach (var part in parts)
if (part.StartsWith(DataSourceMarker, StringComparison.OrdinalIgnoreCase))
filepath = part.Substring(DataSourceMarker.Length);
if (filepath != null)
Dts.TaskResult = (int)ScriptResults.Success;
private void ExcelRefresh(string filename)
object NullValue = System.Reflection.Missing.Value;
var excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.DisplayAlerts = false;
var Workbook = excelApp.Workbooks.Open(
filename, NullValue, NullValue, NullValue, NullValue,
NullValue, NullValue, NullValue, NullValue, NullValue,
NullValue, NullValue, NullValue, NullValue, NullValue);
Workbook.Close(false, filename, null);
Workbook = null;
I kept the code simple for this example, but there are a few different optimizations you can make here:
- Add error handling
- Pass in the excel file name through a variable instead of iterating over each connection manager
- Refresh a specific query within the workbook, rather than calling RefreshAll
- Find your query within the workbook and set the BackgroundQuery property to False to avoid the asynchronous refresh call, and having to sleep