@Amit kumar Yadava , Welcome to Microsoft Q&A, you could try to group by your datatable and use list of string to write all the lines to txt file.
Here is a code example you could refer to.
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Id", GetType(Int32))
dt.Columns.Add("Item", GetType(String))
dt.Columns.Add("Qty", GetType(Int32))
dt.Columns.Add("Date", GetType(DateTime))
dt.Rows.Add(101, "Pen", 5, "2021-10-05")
dt.Rows.Add(102, "Bag", 10, "2021-09-06")
dt.Rows.Add(101, "Copy", 2, "2021-01-05")
dt.Rows.Add(101, "Glue", 10, "2021-03-05")
dt.Rows.Add(102, "Conver", 5, "2021-10-02")
dt.Rows.Add(101, "Cover", 10, "2021-10-01")
Dim result = dt.AsEnumerable().GroupBy(Function(row) row.Field(Of Int32)("Id"))
Dim count As Int32 = 1
Dim arrcount As Int32
Dim sumaccount As Int32
Dim list As New List(Of String)
For Each grp In result
Dim str As String
str = String.Format(" Page{0}", count)
list.Add(str)
str = String.Format("Item Issued For Customer :{0}", grp.Key)
list.Add(str)
list.Add("--------------------------------------------------")
str = String.Format("{0} {1} {2} {3}", dt.Columns(0).ColumnName, dt.Columns(1).ColumnName, dt.Columns(2).ColumnName, dt.Columns(3).ColumnName)
list.Add(str)
list.Add("--------------------------------------------------")
Dim arr = grp.Select(Function(row) row.ItemArray)
For Each line In arr
list.Add(line(0).ToString() + " " + line(1).ToString() + " " + line(2).ToString() + " " + DateTime.Parse(line(3).ToString()).ToShortDateString())
Next
list.Add("--------------------------------------------------")
Dim sum As Int32 = grp.Sum(Function(row) row.Field(Of Int32)("Qty"))
Dim final As String
final = String.Format("Total {0} {1}", arr.Count, sum)
list.Add(final)
list.Add("--------------------------------------------------")
arrcount = arr.Count + arrcount
sumaccount = sum + sumaccount
If count = result.Count Then
str = String.Format("SubTotal {0} {1}", arrcount, sumaccount)
list.Add(str)
list.Add("--------------------------------------------------")
End If
count = count + 1
Next
File.WriteAllLines("test.txt", list)
Tested result:
Hope my code could help you.
Best Regards,
Jack