It would time taking task to help with a sample workflow so sharing logic only. I hope you would be able to code it. Do let me know if you stuck at any step.
Open excel file using Excel Process Scope
Sort the file with Item No column
Use For Each Excel Row activity to iterate through all the rows
Use a variable to store Item number
If previous row item no = current row Sum it and add to a variable
If previous row item no not matching, add a row before current row and add sum variable.
=> Use Read Range Workbook to read the excel and store the output in a data table dtInput.
=> Use the below code in Invoke Code:
dtOutput = New DataTable
dtOutput.Columns.Add("Item No", GetType(String))
dtOutput.Columns.Add("Quantity", GetType(Integer))
' Group data by "Item No" and calculate sums
Dim groupedData = From row In dtInput.AsEnumerable()
Group row By ItemNo = row("Item No").ToString() Into Group
Select New With {
.ItemNo = ItemNo,
.Quantities = Group.Select(Function(r) Convert.ToInt32(r("Quantity"))),
.SumQuantity = Group.Sum(Function(r) Convert.ToInt32(r("Quantity")))
}
' Iterate through grouped data and construct the output DataTable
For Each Group In groupedData
Dim firstRow As Boolean = True
For Each quantity In Group.Quantities
Dim newRow As DataRow = dtOutput.NewRow()
If firstRow Then
newRow("Item No") = Group.ItemNo
firstRow = False
End If
newRow("Item No") = Group.ItemNo
newRow("Quantity") = quantity
dtOutput.Rows.Add(newRow)
Next
' Add the sum row with empty "Item No" cell
If Group.Quantities.Count() > 1 Then
Dim sumRow As DataRow = dtOutput.NewRow()
sumRow("Item No") = String.Empty
sumRow("Quantity") = Group.SumQuantity
dtOutput.Rows.Add(sumRow)
End If
Next
if any grouping found, then just above that and below that group ( which we are doing). Everything is working fine, just need to add one empty row above the group if any grouping found
' Create the output DataTable with columns
dtOutput = New DataTable
dtOutput.Columns.Add("Item No", GetType(String))
dtOutput.Columns.Add("Quantity", GetType(Integer))
' Group data by "Item No" and calculate sums
Dim groupedData = From row In dtInput.AsEnumerable()
Group row By ItemNo = row("Item No").ToString() Into Group
Select New With {
.ItemNo = ItemNo,
.Quantities = Group.Select(Function(r) Convert.ToInt32(r("Quantity"))),
.SumQuantity = Group.Sum(Function(r) Convert.ToInt32(r("Quantity")))
}
' Iterate through grouped data and construct the output DataTable
For Each Group In groupedData
' Add empty row before the group
If Group.Quantities.Count() > 1 Then
Dim emptyRowBefore As DataRow = dtOutput.NewRow()
emptyRowBefore("Item No") = String.Empty
emptyRowBefore("Quantity") = DBNull.Value
dtOutput.Rows.Add(emptyRowBefore)
End If
Dim firstRow As Boolean = True
For Each quantity In Group.Quantities
Dim newRow As DataRow = dtOutput.NewRow()
If firstRow Then
newRow("Item No") = Group.ItemNo
firstRow = False
End If
newRow("Item No") = Group.ItemNo
newRow("Quantity") = quantity
dtOutput.Rows.Add(newRow)
Next
' Add the sum row with empty "Item No" cell
If Group.Quantities.Count() > 1 Then
Dim sumRow As DataRow = dtOutput.NewRow()
sumRow("Item No") = String.Empty
sumRow("Quantity") = Group.SumQuantity
dtOutput.Rows.Add(sumRow)
End If
Next
You may try one more way like the below with LINQ query.
Code:
(From row In dtInput.AsEnumerable
Group row By ItemNumber = row("Item No") Into grp =Group
Let Sum = grp.AsEnumerable.sum(Function(grpRow) CInt(grpRow("Quantity").tostring))
Let UniqueRows = grp.Count
Let dtAddedNewRow = grp.Concat({dtOutput.NewRow}).CopyToDataTable
From grpItem In dtAddedNewRow
Select dtOutput.Rows.Add(
If( dtAddedNewRow.Rows.IndexOf(grpItem) < UniqueRows,
grpItem.itemArray,
If(grp.count>1,
{"",Sum.tostring},
{""} )))
).copyToDataTable
Post here for your any issue, in case if you are trying.