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

How to Load Excel File Name and Sheet Name with Data to SQL Server in SSIS Package - SSIS Tutorial

$
0
0

Scenario: Download Script

You need to load Excel file/s with single or multiple sheets to SQL Server Table. As you will loading the records on daily basis, you would like to keep the information which records are loading from which Excel file and Sheet. You need to create an SSIS Package that should be able to load data from Single/Multiple Excel files with single/Multiple Sheets and also log Excel file and Excel Sheet in table.

Here are my couple of Excel sample files with single and multiple Sheets.
How to load Excel files with single or multiple sheets to SQL Server Table with File Name and Sheet Name in SSIS Package by using Script Task

Solution:

I am going to create dbo.Customer Table to load these Excel Files data. As you can see the excel sheets, I have id, name and dob columns. The table dbo.Customer is going to have these columns and also FileName and SheetName so we can save Excel file name and Sheet Name from which data is loaded.

Createtable dbo.Customer(
id int,
name VARCHAR(100),
dob date,
FileName VARCHAR(100),
SheetName VARCHAR(100))



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

Create variables in SSIS Package to Save Excel File Name and Sheet Name with Data in SQL Server Table



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 Data with File Name and Sheet 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.
How to use variables in Script Task in SSIS Package to Load Excel Files Data with File Name and Excel Sheet


Step 4: Add Script to Script task Editor in SSIS To load Excel Data with File Name and Sheet Name
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();

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;

string filename = "
";
filename = 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 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 + ",'"+filename+"' AS FileName"+ ",'"+sheetname+"' AS SheetName 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: Run SSIS Package to Load Excel Data with Excel File and Excel Sheet Information
Save the script and Close the Script Task Editor. Run your SSIS Package. It should load data from Excel Sheets to your Table and also insert Excel file name and sheet name from where the data is loaded. 

I executed the SSIS Package and here is my output.
How to load Excel File Name and Sheet Name with Data to SQL Server Table by using SSIS Package- Script Task C# Scripting Language


As you can see that data is loaded from two Excel files. First Excel file had two sheets and data is loaded from both. The second Excel file had only single sheet and you can see in above picture data is loaded with file name and sheet name.


Viewing all articles
Browse latest Browse all 1976

Trending Articles