How to convert data table into string with preferable reading format?

Hello to experts, please kindly help. I would like to know how to convert the data table into a string with a preferable reading format.

I have done the read range workbook from an Excel file and converted it to dt_description.
In dt_description, we got multiple columns. | Name | Age | Gender | Number | Hight | Weight | Description | Type |
| Marcel| 55 | Male | 0800 | 166 | 67 | Like to drink coffee | Flat White |

I have tried to use the output data table to convert it to string and type into a column to check the format, but it seems very difficult to read.

Is there any chance that I can convert it into the format below?

Name
Marcel

Age
55

Gender
Male

Number
0800

Hight
166

Weight
67

Description
Like to drink coffee

Type
Flat White

@Marcellee,

Yes we can do this but we will have to code through this. Use For Each Row in DataTable activity and keep appending the Column name and value to the output string.

It’s feasible only for smaller datatable. If have to do it on large datatable, it will be time consuming.

Hi, thanks for replying to me. May I know how should I code it? I have zero coding background. Is there any possibility for me to learn it?

Hi @Marcellee ,
Use output data table activity to convert datatable to string.
Reference
https://activities.uipath.com/docs/datatable-output-data-table

After that using write text file* activity you can write into text file.
Reference
https://activities.uipath.com/docs/file-write-text-file

Refer these links, hope this helps…

1 Like

Hi,

How about the following sample?

dict = dt.Columns.Cast(Of DataColumn).ToDictionary(Function(dc) dc.ColumnName,Function(dc) dt.Rows(0)(dc).ToString)

Sample
Sample20241016-3.zip (9.5 KB)

Regards,

1 Like

Hi,
I tried to open up the two links, but all been redirected to the UiPath activity webpage.

@Marcellee , Ok check for youtube link.

1 Like

Hi, thanks for your reply.
Yes, what the message box shows is the preferable format that I want.
May I know in detail how and why you input the codes? Please.

Thank you for adding the links for me.

1 Like

Hi,

It seems your requirement is to handle set of column name and data in the first row. So, first, I create dictionary which has column name as key and data as value by the following expression.

dict = dt.Columns.Cast(Of DataColumn).ToDictionary(Function(dc) dc.ColumnName,Function(dc) dt.Rows(0)(dc).ToString)

Then, dict has key value pair like {“Name” , “Marcel”} , {“Age”, “55”} …

Next, to convert preferable string format, concatenate key and value with linebreak, then join all the part with linebreak.

String.Join(vbcrlf,dict.Select(Function(kv) kv.Key +vbcrlf+kv.Value+vbcrlf))

Finally, we can get the string.

Hope this helps you.

Regards,

1 Like

Hi, thank you! Really appreciate your help. I will try this out.

Hi, I tried to mimic your method, but something went wrong. Please kindly help.

Kindly find my scripts as below


Hi,

It seems missing Rows.

And type of dict_case should be Dictionary<string,string> (not DictionaryBase).

Also check the above sample project.

Regards,

1 Like