Quantcast
Channel: Welcome To TechBrothersIT
Viewing all articles
Browse latest Browse all 1974

How to import Data from Excel Files for specific Sheet Name to SQL Server Table in SSIS Package - SSIS Tutorial

$
0
0

Scenario: Download Scripts

Let's say we get Single or Multiple Excel Files in source folder. The Excel file/s can have multiple sheets. But we are interested to load only single sheet with name "CustomerSheet" from each of the Excel File. How will you create your SSIS Package.

Sample Excel files with multiple sheets and containing one Sheet with CustomerSheet Name.

How to load Excel Sheet with Specific Name from Excel Files to SQL Server Table in SSIS


Solution:

We will be using Script Task with C# scripting Language in our SSIS Package to load only single Sheet with name from single or multiple excel files to SQL Server Table.

Go ahead and create new SSIS Package and rename to Load Data from Excel Files for specific sheet by using Name to SQL Server Table.

create dbo.Customer Table in your Database so we can load CustomerSheet data to it.

CREATETABLE dbo.Customer (
id INT
,name VARCHAR(50)
,dob DATE
)
GO

Step 1: Create Variable in SSIS to Make your SSIS Package Dynamic
Create below variables in your SSIS Package
FolderPath: In this variable you will save the path from where you would like to read Excel Files
SchemaName: Schema of your Destination Table
TableName: Table in which you want to load the data
SheetNametoLoad: Provide the Sheet Name you would like to load from each Excel File.


How to load Excel Sheet to SQL Server Table by using Excel Sheet Name in SSIS Package- Script Task


Step 2: Create ADO.NET Connection in SSIS Package to use in Script Task
Create ADO.NET Connection Manager so we can use in Script Task to Load data from Excel Sheets to SQL Server Table.

Create ADO.NET Connection in SSIS Package to use in Script Task to Load Excel Sheet By Name

Step3: Add Variables to Script Task to use from SSIS Package
Bring the Script Task on Control Flow Pane in SSIS Package and open by double clicking Check-box in front of variable to add to Script Task.

Add Variable to Script Task to use from SSIS Package for Excel Sheet Load by name - Script Task C# Excel


Step 4: Add Script to Script task Editor in SSIS To load Specific Excel Sheet to SQL Server Table
Click Edit Button and it will open Script Task Editor.
Under #region Namespaces, I have added below code
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;


Under public void Main() { 
I have added below code. 

string FolderPath = Dts.Variables["User::FolderPath"].Value.ToString();
string TableName = Dts.Variables["User::TableName"].Value.ToString();
string SchemaName = Dts.Variables["User::SchemaName"].Value.ToString();
string SheetNameToLoad = Dts.Variables["User::SheetNametoLoad"].Value.ToString();

var directory = new DirectoryInfo(FolderPath);
FileInfo[] files = directory.GetFiles();

//Declare and initilize variables
string fileFullPath = "";


SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["DBConn"].AcquireConnection(Dts.Transaction) as SqlConnection);

//Get one Book(Excel file at a time)
foreach (FileInfo file in files)
{
fileFullPath = FolderPath + "\\" + file.Name;

//Create Excel Connection
string ConStr;
string HDR;
HDR = "
YES";
ConStr = "
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileFullPath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
OleDbConnection cnn = new OleDbConnection(ConStr);

//Get Sheet Name
cnn.Open();
DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetname;
sheetname = "";
//Only read data from provided SheetNumber

foreach (DataRow drSheet in dtSheet.Rows)
{

if (drSheet["TABLE_NAME"].ToString().Contains("$"))
{
sheetname = drSheet["TABLE_NAME"].ToString();
// MessageBox.Show(sheetname);

//Load the Data if Sheet Name Matches
if(sheetname==SheetNameToLoad+"$")
{



//Load the DataTable with Sheet Data so we can get the column header
OleDbCommand oconn = new OleDbCommand("select top 1 * from [" + sheetname + "]", cnn);
OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
DataTable dt = new DataTable();
adp.Fill(dt);
cnn.Close();

//Prepare Header columns list so we can run against Database to get matching columns for a table.
string ExcelHeaderColumn = "";
string SQLQueryToGetMatchingColumn = "";
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i != dt.Columns.Count - 1)
ExcelHeaderColumn += "'" + dt.Columns[i].ColumnName + "'" + ",";
else
ExcelHeaderColumn += "'" + dt.Columns[i].ColumnName + "'";
}

SQLQueryToGetMatchingColumn = "select STUFF((Select ',['+Column_Name+']' from Information_schema.Columns where Table_Name='" +
TableName + "' and Table_SChema='" + SchemaName + "'" +
"and Column_Name in (" + @ExcelHeaderColumn + ") for xml path('')),1,1,'') AS ColumnList";

// MessageBox.Show(SQLQueryToGetMatchingColumn);
// MessageBox.Show(ExcelHeaderColumn);


//Get Matching Column List from SQL Server
string SQLColumnList = "";
SqlCommand cmd = myADONETConnection.CreateCommand();
cmd.CommandText = SQLQueryToGetMatchingColumn;
SQLColumnList = (string)cmd.ExecuteScalar();

// MessageBox.Show(" Matching Columns: " + SQLColumnList);


//Use Actual Matching Columns to get data from Excel Sheet
OleDbConnection cnn1 = new OleDbConnection(ConStr);
cnn1.Open();
OleDbCommand oconn1 = new OleDbCommand("select " + SQLColumnList + " from [" + sheetname + "]", cnn1);
OleDbDataAdapter adp1 = new OleDbDataAdapter(oconn1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
cnn1.Close();


//Load Data from DataTable to SQL Server Table.
using (SqlBulkCopy BC = new SqlBulkCopy(myADONETConnection))
{
BC.DestinationTableName = SchemaName + "." + TableName;
foreach (var column in dt1.Columns)
BC.ColumnMappings.Add(column.ToString(), column.ToString());
BC.WriteToServer(dt1);
}

}
}

}

}


Step 5: Test your SSIS Package to Load Excel Sheet by Name in SSIS Package by using Script Task 

Save all the changes in Script Task Editor and exit. Run your SSIS Package and it should be able to load Excel sheet from each of the excel file which name you have provided in the FileNameToLoad variable.

In my case, I have provided CustomerSheet and it loaded both sheets data from two excel files as shown below.
How to load Excel Sheet by name from Single or Multiple Excel Sheets to SQL Server Table in SSIS Package by using Script Task by using C# scripting Language




Things we learnt in this post
How to load Data from Excel Files to SQL Server Table by using C#
How to read data from Specific Sheet by using Sheet Name from Excel File and load to SQL Table
How to read Excel File Name in SSIS Package
How to Check matching Header Column Names to SQL Server Table by using C sharp
How to save scalar string value from SQL Server Query to Variable in C Sharp
How to use BulkCopy to load data from DataTable to SQL Server Table
How to Map Columns Dynamically in BulkCopy C Sharp



Viewing all articles
Browse latest Browse all 1974

Trending Articles