How to convert datatable to text file by adding individual lines on the page separated by one or two “& vbcrlf &” for line spaces

Hi All,

I need to create text file and add the datatable values into it by adding individual lines on the page separated by one or two “& vbcrlf &” for line spaces.

Please find below format

Input Datatable :
ID Name company name
1 abc xyz

Output text file: (which is required)

ID 1
Name abc
Company name xyz

Can you please on the same.

Thanks

Assign Activity
strFlat | String =

(From d In dtData.AsEnumerable
Let rs = d.ItemArray.Select(Function (x,i) String.Format("{0} {1}", d.Table.Columns(i).ColumnName, x.ToString))
From c In rs
Select v=c).Aggregate(Function (x,y) String.Format("{0}{1}{2}", x, vbCrLf, y))

grafik

For sure we can also model with essential activities

1 Like

Hey @Nancy29
you can also try this:

columnHeaders = dtExample.Columns.Cast(Of DataColumn)().[Select](Function(col) col.ColumnName & " ").ToList()

columnData = dtExample.Columns.Cast(Of DataColumn)().[Select](Function(col) columnHeaders(dtExample.Columns.IndexOf(col)) & String.Join(" ", dtExample.AsEnumerable().[Select](Function(row) row(col).ToString()))).ToList()

image
image

working workflow you can find here:
BlankProcess80.zip (2.9 KB)

1 Like

Hi @Nancy29

→ Build Data Table
image
Output= dt_Input
→ Use the below query in Assign activity:

Output= String.Join(vbCrLf + vbCrLf,
    dt_Input.AsEnumerable().Select(
        Function(row) $"ID: {row.Field(Of String)("ID")}" + vbCrLf +
                      $"Name: {row.Field(Of String)("Name")}" + vbCrLf +
                      $"Company Name: {row.Field(Of String)("Company Name")}"
    )
)

Output is of DataType System.String
→ Use Write Text File activity to write the output to text file.

Text file Output:

Workflow:

xaml:
Sequence16.xaml (10.6 KB)

Hope it helps!!

1 Like

Thank you everyone. Will check and work with above methods.

It will definitely help!!

Regards,

Hi @Nancy29

If you find the solution for the query after working it out, please mark the post as the solution to close the loop.

Happy Automation!
Regards

Thanks All.

It worked for me!

Regards,

1 Like

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