Home » excel » sql server – SSIS Excel Import – Worksheet variable OR wildcard?

sql server – SSIS Excel Import – Worksheet variable OR wildcard?

Posted by: admin April 23, 2020 Leave a comment


I have a SSIS data import package that uses a source Excel spreadsheet and then imports data into a SQL Server database table. I have been unsuccessful in automating this process because the Excel file’s worksheet name is changed every day. So, I have had to manually change the worksheet name before running the import each day. As a caveat, there will never be any other worksheets.

Can I make a variable for the worksheet name?
Can I use a wildcard character rather than the worksheet name?
Would I be better off creating an Excel macro or similar to change the worksheet name before launching the import job?

How to&Answers:

I use the follow script task (C#):

System.Data.OleDb.OleDbConnection objConn;
DataTable dt;

string connStr = ""; //Use the same connection string that you have in your package
objConn = new System.Data.OleDb.OleDbConnection(ConnStr);

dt = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbShemaGuid.Tables,null);

foreach(DataRow r in dt.Rows)
   //for some reason there is always a duplicate sheet with underscore.
   string t = r["TABLE_NAME"].ToString(); 

   //Note if more than one sheet exist this will only capture the last one
       Dts.Variables["YourVariable"].Value = t;

And then in SSIS, I add another variable to build my SQL.

new variable “Select * from [” + “Your Variable” + “]”

Finally set your datasource to that SQL variable in Excel Source.


This works perfectly for me with the same scenario, in case it helps you or someone else:

Required package level string variables 2:

  • varDirectoryList – You will use this inside SSIS for each loop variable mapping

  • varWorkSheet – This will hold your changing worksheet name. Since you only have 1, it’s perfect.

Set up:

  • a. Add SSIS For Each Loop
  • b. Excel Connection Manager (connect to first workbook as you test, then at the end you will go to properties and add inside expression “Excel File Path” your varDirectoryList. Set DelayValidation True as well as your Excel Source task. *This will help it go through each workbook in your folder)
  • c. Inside your For Each Loop add a Scrip Task C#, title it “Get changing worksheet
    name into variable” or your preference.
  • Data Flow Task with your Excel Source to SQL Table Destination.

In your Scrip Task add this code:

using System.Data.OleDb;

    public void Main()
        // store file name passed into Script Task
        string WorkbookFileName = Dts.Variables["User::varDirectoryList"].Value.ToString();

        // setup connection string
        string connStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"EXCEL 12.0;HDR=Yes;IMEX=1;\"", WorkbookFileName);

        // setup connection to Workbook
        using (var conn = new OleDbConnection(connStr))
                // connect to Workbook

                // get Workbook schema
                using (DataTable worksheets = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null))
                    // in ReadWrite variable passed into Script Task, store third column in the first 
                    // row of the DataTable which contains the name of the first Worksheet
                    Dts.Variables["User::varWorkSheet"].Value = worksheets.Rows[0][2].ToString();

                    //Uncomment to view first worksheet name of excel file. For testing purposes.
            catch (Exception)


After you have this set up and run, you will get a message box displaying the changing worksheet names per workbooks.

  • If you are using Excel Source SQL Command you will need a 3rd string
    variable like: varExcelSQL and inside that an expression like: SELECT
    columns FROM [‘varWorkSheet$’] which will dynamically change to match
    each workbook. You may or may not need the single quotes, change as
    needed in varExcelSQL.
  • If you are not using Excel Source SQL and just loading straight from
    the Table; go into Excel Source Properties –> AccessMode –>
    OpenRowSet from Variable –> select varWorkSheet.

That should take care of it, as long as the column structures remain the same.

If you happen to get files where it has multi data types in one column; you can use IMEX=1 inside your connection string which forces the datatypes to DT_WSTR’s on import.

Hope this helps 🙂


If you are using SSIS to import the sheet you could use a script task to find the name of the sheet and then change the name or whatever else you needed to do in order to make it fit the rest of your import. Here is an example of finding the sheet I found here

Dim excel As New Microsoft.Office.Interop. Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop. Excel.Workbook
Dim wSheet As Microsoft.Office.Interop. Excel.Worksheet

wBook = excel.Workbooks.Open 
wSheet = wBook.ActiveSheet()

For Each wSheet In wBook.Sheets

On the MsgBox line is where you could change the name or report it back for another process

<a href="https://exceptionshub.com/category/sql-server”>More Answers>