Home > SSIS, SSIS programming > Programming Foreach Loop Container – Enumerating Excel Sheets

Programming Foreach Loop Container – Enumerating Excel Sheets

In my previous article named “Programming Foreach Loop Container – Enumerating Excel Files”, we discussed about enumerating Excel files in a folder and load data from each excel file to the destination by using Foreach File enumerator.

But there may be cases where we may have data in the form of sheets in a single excel file. For example, the total sales of study materials over different countries.

To load all the sheets from a single excel file in Foreach Loop Container, we have to use “Foreach ADO.NET Schema Rowset Enumerator”. This special enumerator is used to enumerate through schema information about a data source i.e. the schema information supported by the OLEDB provider. For example, to get list of tables, schemas, column information, constraints, etc from a database.

Excel Sheets example

Below you will see step by step operations to configure the package for enumerating sheets from a excel file.

Step 1: Create a package with the basic components and tasks. Refer my earlier article for details. Programming Foreach Loop Container – Enumerating Excel Files

Step 2: Create a variable named “CurrentExcelSheet” of String type with default value as “India$” (one of the sheet name) to avoid any validation error.

Step 3: Create Connection manager for source (Excel Connection manager) and for the destination (OLEDB connection manager)

Connection Managers

Step 4: Create ADO .Net connection manager to access the excel file in the Foreach ADO.NET Schema Rowset Enumerator.

Right click on the connection manager area and select “New ADO .NET Connection

ADO .net connection


In the connection manager dialog box, select “Microsoft Jet4.0 OLE DB Provider” so that we can access Excel file by this OLE DB driver.

Microsoft Jet4.0 OLE DB Provider

Then in the “Database file name” section select the Excel file that contains information about all the sales. (If necessary select “All Files *.*” in the file open dialog box.)

Database file name

Leave the User name and password fields as it is. Click on “All” pane at left side and set the value for “Extended Properties” to “Excel 8.0”.

Extended Properties

Then test the connection and press OK.

Step 5: Create a “Foreach Loop Container” and set the “Enumerator” in Collection pane to “Foreach ADO.NET Schema Rowset Enumerator”. Here two more properties you have to set. They are:

a) Connection: Select an ADO.NET connection manager in the list. Here in our case, the connection manager will be “Ado .net for Excel” that we created above.

b) Schema: Select the schema to enumerate. For our case we set “Tables” as we need to enumerate all the sheets of the given excel file.

Keep the “Set Restrictions” settings as it is. For Each Loop Editor

Step 6: Then we have to set the Variable Mappings pane. Here we will set the “User::CurrentExcelSheet” variable to index 2. Here index 2 is specifically used because the table information of the excel sheet is at the 3rd column of the resultset generated by the “Foreach ADO.NET Schema Rowset Enumerator” and the columns of the resultset have 0 based indexing.

Step 7: Create the data flow task name “Sales” where we have one source named “Excel Source (Sales)”, but the table information for the source will be fetched from the variable “User::CurrentExcelSheet” so that at each iteration, we will get the new sheet information from the excel file.

Data Flow Task

For this we have to set the “Data access mode:” to “Table name or view name variable” and the variable name to “User::CurrentExcelSheet”.

Excel Source Editor

Step 8: Create the destination component and all other components as per the requirement and run the package. For more information about the other components, refer my article named “SSIS Programming Basic”.

Control Flow

So we successfully executed the package for extracting data from different sheets of a excel file.

Let’s have a look on the procedures to create the above logic by using Integration Service Programming in SSIS. The code will go in the same way as my previous article “Programming Foreach Loop Container – Enumerating Excel Files” except some modifications in the areas of Enumerator where we are using “Foreach ADO.NET Schema Rowset Enumerator


In the Excel source area the data is extracted from a variable instead of Open Rowset. The code is self descriptive and easy to understand. The code is given in both C# .net and VB .net language for both SQL Server 2005 and SQL Server 2008.

Add the below References before developing the code.

Microsoft.SqlServer.Dts.Design
Microsoft.SqlServer.DTSPipelineWrap
Microsoft.SQLServer.DTSRuntimeWrap
Microsoft.SQLServer.ManagedDTS

Programming Integration Service in SQL Server 2005

Expand image C# .Net Expand
Expand image VB .Net Expand

Programming Integration Service in SQL Server 2008

Expand image C# .Net Expand
Expand image VB .Net Expand

To make your work more easy and effortless, SQL Lion team comes up with a unique tool named “SSIS Component Explorer” that will help you in retrieving the Creation Name, Component Class ID, etc about any component like Control Flow Tasks, Data Flow Transformations, Connections Managers, Log Providers, etc in SSIS package. It also provides code snippets for each component.
Download
the free version from here.
Categories: SSIS, SSIS programming
  1. August 6th, 2009 at 19:38 | #1

    Do you have more information on your tool “SSIS Component Explorer”?

    http://www.perpetualdynamics.com

  2. August 7th, 2009 at 01:01 | #2

    Please visit the below link, download the software “SSIS Component Explorer” and explore it….

    I hope you will get enough information.
    Thanks

  3. ziegheart
    April 6th, 2010 at 18:22 | #3

    thank you for exam
    but i need add the country name from excel sheet(on you exam = india,australia,cananda,japan)continue the last column into the destination
    help me please

    ps. i’m not strong in english ,sorry

  4. Reddeppa
    July 20th, 2010 at 11:34 | #4

    Excellent Explaination

  1. No trackbacks yet.