I have an excel which has data in below format … ID column occurs three times & we need to collate all id’s into one & their corresponding adjacent component column values …
ID | Comp1 | ID | Comp2 | ID | Comp3 | |||
---|---|---|---|---|---|---|---|---|
12 | 1232 | 1 | 34565 | 8 | 90 | |||
1 | 34 | 2 | 63 | 1 | 45 | |||
5 | 2455 | 5 | 23456 | 6 | 45 | |||
6 | 67 | 12 | 12345 | 5 | 45 |
Now I need result something like this
ID | Comp1 | Comp2 | Comp3 |
---|---|---|---|
12 | 1232 | 12345 | 0 |
1 | 34 | 34565 | 45 |
5 | 2455 | 23456 | 450 |
6 | 67 | 0 | 45 |
For that steps I did
I read the maininputfile as it is without headers - since there are duplicates & blank column & I got the indexes of ID columns like list - {0,3,7} in this case & I tried to get the final result table using c# code .
(We know that after first occurrence its always comp1 value , second occurence of empid followed by comp2 & similarly comp3. We only will have 3 occurences of ID column).
C# code I used in invoke code
public static void Main(DataTable dt1, System.Collections.Generic.List emplist, out DataTable resultTable)
{
resultTable = new DataTable();
resultTable.Columns.Add(“Emp ID”, typeof(int));
resultTable.Columns.Add(“OT”, typeof(int));
resultTable.Columns.Add(“NSA”, typeof(int));
resultTable.Columns.Add(“ASA”, typeof(int));
try
{
// Iterate through the rows of the datatable
foreach (DataRow row in dt1.Rows)
{
// Iterate through each Emp ID column index
for (int iteration = 1; iteration <= 3; iteration++)
{
int empIdIndex = emplist[iteration - 1];
int componentColumnIndex = empIdIndex + iteration;
int componentValue = 0;
if (componentColumnIndex < row.ItemArray.Length && row[componentColumnIndex] != DBNull.Value)
{
componentValue = Convert.ToInt32(row[componentColumnIndex]);
}
if (iteration == 1)
{
DataRow newRow = resultTable.NewRow();
newRow["Emp ID"] = Convert.ToInt32(row[empIdIndex]);
newRow["OT"] = componentValue;
resultTable.Rows.Add(newRow);
}
else
{
DataRow lastRow = resultTable.Rows[resultTable.Rows.Count - 1];
lastRow[iteration == 2 ? "NSA" : "ASA"] = componentValue;
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error occurred: " + ex.Message);
}
}
Its not throwing error or its not even giving output. Please help me if this code format is fine for the requirement or if I’m missing something for invoke code