Split Datatable into 4 different tables

Hey guys,

So i generate a datatable with 100+ rows every week. Now i’d like to split that table into 4 parts with about equal numbers of rows in each. Found some rather complicated solutions but is there a simple way to do this?

And also a random question: What would be the best language to learn to improve in UIpath? C#?


Filter data table could be used in such case

Hi @thomasb82

Did u check this

Split one excel files into multiple files based on 100 rows per file

lets assume following variables:

NoOfSegments is indicating how many splits we do want have

Doing an integer division to find out how many rows will be in a split (we cannot fetch a rows count with fractions e.g. 14.25 rows

with following LINQ we can produce a list of datatables with the splits:

(From i In Enumerable.Range(0,NoOfSegments)
Let opart = dtData.AsEnumerable.Skip(i*FullSegmentSize).toList
Let spart = If(i = (NoOfSegments - 1), opart, opart.Take(FullSegmentSize)).toList
Select spart.CopyToDataTable).toList

line1 - creating a sequence with the segment numbers (0,1,2,3)
line2 - skip rows acordingly the looped segment
line3 - take rows of an amount of FullSegmentSize or take all remaining rows when it is the last segment

line4 - Copy the segment rows to a DataTable, add it to returned tablelist

find starter help here:
DT_Slice_ByNSegments.xaml (7.1 KB)

1 Like

Hi @thomasb82

Try adopting the following steps:

  1. Read the Excel file to a data table.
  2. for each row the data table
  3. check if your row number ( counter ) is > than datatable.rows.count/2. If yes, put the res on 2. new datatable. else put it in 2nd data table. Similary inherit if else for addition split of data.
  4. Save both the datatable in 2 ore more difference Excels files

Many great solutions, will evaluate and get back to ya’ll, thanks.

1 Like

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