How to add multiple arrays in to single DataTable

I have 4 array strings , want to add them in Single Datatable. Any leads helps

Arr1={1,2,3,4}
Arr2={abc,def,fgh,jks)
Similarly Arr3, and arr4
Thanks

Hi @Kniranjan555

Welcome to Community!!

  1. Create a DataTable: Create a new DataTable to store the combined data.
  2. Add Columns: Add columns to the DataTable to match the data types of the arrays.
  3. Loop Through Arrays: Use a loop (such as a For Each loop) to iterate through each array and add the values to the DataTable.

Regards,

Hi @Kniranjan555

Create a datatable by using build datatable activity.
Create a array of String variable and store {Arr1;Arr2;Arr3;Arr4}.
Use for each to iterate the above variable.
Inside for each use another for each to iterate the Arr1
Use add data row to add the data to the rows to the datatable.
At last use the write range workbook activity to write the datatable to excel.

Find the below workflow that I built with your condition.

Expressions_Practice.xaml (14.3 KB)

Output excel -
image

Hope it helps!!

Do you want the combined output to be Arr1 in one column, Arr2 in another column, in the same row?

Do you want 8 columns with values 1, 2, 3, 4, abc, def, fgh, jks?

Do you want 4 columns with two rows, one row for each array?

Something else? You have to show us what your desired output looks like or we can’t help.

@Kniranjan555

  1. Build datatable with 4 columns
  2. For each activity with arr1 and assign a indexvar to the index property in for each
  3. Inside loop use add datarow with datatbe created in step1 and in array rows give {currentItem,arr2(indexvar,arr3(indexvar),arr4(indexvar)}

Assumption is all arrays have similar number of items

Cheers

@Kniranjan555

  1. Create a new DataTable variable (let’s call it “dtCombined”).
  2. Use a For Each activity to loop through each array. You can use the “Arr1”, “Arr2”, “Arr3”, and “Arr4” variables as input for the For Each activity.
  3. Inside the For Each loop, use an Assign activity to create a new DataRow and populate it with the values from the current array element.
  • For example, if you are iterating through “Arr1”, you can use the following Assign activity:
newRow = dtCombined.NewRow()
newRow("Column1") = item.ToString() ' Assuming you want to add the value to the first column
dtCombined.Rows.Add(newRow)
  • Repeat the Assign activity for each array to add their values to the corresponding columns in the DataRow.
  1. After the For Each loop is completed, you will have all the array values stored in the DataTable “dtCombined”.

Hi @Kniranjan555
Use Build data Table activity and create the volumns and store it an variable say dt_combineddata.
2. Intialize all your 4 arrays.
E. Use For each loop to iterate through each say and add the items using Add Data Row activity and store in dt_combineddata variable.
4. Use Write Range Workbook avtivity at last to store all the combined data in excel.

Hope it helps!!

@Kniranjan555

1 Like

Build DataTable - configure the 4 columns - dtResult

Assign Activity:
dtResult =

(From i in Enumerable.Range(0,arr1.Length)
Let ra = new Object(){arr1(i),arr2(i),arr3(i),arr4(i)}
Select r = dtResult.Rows.Add(ra)).CopyToDataTable

When arr1,2,3,4 are of different length we can modify and handle as well by filling up with null values

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.