Hi All,
I have an Excel source which is loading data to an OLE DB destination. Before loading data, there is a script task which finds out the first column and the first row within the excel sheet. The logic is that wherever the word 'Table' is present in the file,
it should be the first column.
For some reason, the script task is not picking the correct start and end columns. For now, I have hard coded the values but this needs to be fixed asap.
Below is my code :
/* Microsoft SQL Server Integration Services Script Component
* Write scripts using Microsoft Visual C# 2008.
* ScriptMain is the entry point class of the script.*/
using System;
using System.Data;
using System.Windows.Forms;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
publicclassScriptMain :UserComponent
{
//class variables declaration
int intRow;
int intColumn;
int intStartRow;
int intEndRow;
int intStartColumn;
bool bolCheck;
publicoverridevoid PreExecute()
{
base.PreExecute();
//initializing the variables
intRow = 0;
intColumn = 0;
bolCheck =false;
}
int Asc(char chrReturnMyAscii)
{
//return the character's ASCII value of the given character
return (int)System.Convert.ToChar(chrReturnMyAscii);
}
Char Chr(int intReturnMyChar)
{
//return the character of the given character's ASCII value
returnConvert.ToChar(intReturnMyChar);
}
publicoverridevoid Input0_ProcessInputRow(Input0Buffer Row)
{
//incrementing the row counter each time a row is processed.
intRow = intRow + 1;
if (intRow > 12) {
MessageBox.Show(intRow.ToString()); }
//checking if the "Table" word has already been found.
if (bolCheck == false)
{
//checking if the end of rowset has been reached or not.
if (Row.EndOfRowset() ==
false)
{
//resetting column counter to 0 each time a row is processed.
intColumn = 0;
//loop to check the columns for the current row to find the "Table" word.
foreach (System.Reflection.PropertyInfo piColumnin Row.GetType().GetProperties())
{
//incrementing the column counter each time a column is processed.
intColumn = intColumn + 1;
if (intRow > 12)
{MessageBox.Show(intColumn.ToString());
MessageBox.Show((Convert.ToString(piColumn.GetValue(Row,null))));
}
MessageBox.Show((Convert.ToString(piColumn.GetValue(Row,null))));
//checking if the column's value is "Table"
if (Convert.ToString(piColumn.GetValue(Row,null)) == "Table")
{
MessageBox.Show((Convert.ToString(piColumn.GetValue(Row,null))));
//setting the variables when the "Table" word is found in the source file.
bolCheck =true;
intStartRow = intRow + 1;
intStartColumn = intColumn;
MessageBox.Show(intStartColumn.ToString());
MessageBox.Show(intStartRow.ToString());
MessageBox.Show(bolCheck.ToString());
break;
}
}
}
}
//storing the last row number into the variable.
intEndRow = intRow + 1;
//MessageBox.Show(intEndRow.ToString());
}
publicoverridevoid PostExecute()
{
int stepno = 0;
try
{
base.PostExecute();
//declaration of the variables used in the PostExecute method.
int intStartColumnIndex;
string strStartColumnDesc;
int intEndColumnIndex;
string strEndColumnDesc;
int intStartColumnNumber;
int intTotalColumn;
// Lock variable for read
VariableDispenser variableDispenser = (VariableDispenser)this.VariableDispenser;
variableDispenser.LockForRead("User::vintTotalColumns");
IDTSVariables100 vars;
variableDispenser.GetVariables(out vars);
// Fill the internal variable with the value of the SSIS variable
intTotalColumn = (int)vars["User::vintTotalColumns"].Value;
stepno = 0;
//calculating and storing the actual column number where the word "Total" is found.
intStartColumnNumber =Convert.ToInt16(Math.Round(Convert.ToDecimal(intStartColumn + 1) / 2));
intStartColumnIndex = intStartColumnNumber % 26;
MessageBox.Show(intStartColumnNumber.ToString());
if (intStartColumnIndex == 0)
{
intStartColumnIndex = 26;
}
stepno = 1;
//logic to get the column name of the columns.
if (Convert.ToInt16(Convert.ToInt16(intStartColumnNumber - intStartColumnIndex) / 26) == 0)
{
stepno = 2;
strStartColumnDesc =Convert.ToString(Chr(Asc('A') + (intStartColumn - 1)));
//Convert.ToString(Chr(Asc('A') + (intStartColumn - 1) / 2));
MessageBox.Show(strStartColumnDesc.ToString());
}
else
{
stepno = 3;
strStartColumnDesc =Convert.ToString(Chr(Asc('A') +Convert.ToInt16((intStartColumnNumber - intStartColumnIndex) / 26) - 1) + Chr(Asc('A') + intStartColumnIndex - 1));
}
intEndColumnIndex = (intStartColumnNumber +Convert.ToInt16(intTotalColumn) - 1) % 26;
stepno = 4;
if (intEndColumnIndex == 0)
{
intEndColumnIndex = 26;
}
if (Convert.ToInt16(((intStartColumnNumber +Convert.ToInt16(intTotalColumn)) - intEndColumnIndex) / 26) == 0)
{
stepno = 5;
strEndColumnDesc =Convert.ToString(Chr(Asc('A') + intStartColumnNumber +Convert.ToInt16(intTotalColumn) - 1));
MessageBox.Show(strEndColumnDesc);
}
else
{
stepno = 6;
strEndColumnDesc =Convert.ToString(Chr(Asc('A') +Convert.ToInt16((intStartColumnNumber +
Convert.ToInt16(intTotalColumn) - intEndColumnIndex) / 26) - 1)) + Chr(Asc('A') + intEndColumnIndex - 1);
MessageBox.Show(strEndColumnDesc);
}
stepno = 7;
//storing the values into the packages variables.
this.ReadWriteVariables["vstrStartColumnTOT"].Value ="G"; // strStartColumnDesc;
this.ReadWriteVariables["vstrEndColumnTOT"].Value ="AW"; // strEndColumnDesc;
this.ReadWriteVariables["vintStartRowTOT"].Value = intStartRow;
this.ReadWriteVariables["vintEndRowTOT"].Value = intEndRow;
// Unlock variable
vars.Unlock();
}
catch (Exception ex)
{//Dts.Events.FireError(0, "Script Task Example", ex.Message ,0);
bool pbCancel =false;
this.ComponentMetaData.FireError(0,"", "An error occurred: at"+ stepno.ToString() + ex.Message.ToString(),"", 0, out pbCancel);
}
}
}
Please help!
Thanks,
A