Convert dictionary to datatable to send to excel

Hi All,

I want to send the contents of a dictionary to Excel. I was wondering if there is a quick way of converting a dictionary to a datable so that I can then use write range, or should I just loop through the dictionary and write the dictionary key and data into the Excel worksheet line by line.

Many thanks,

Clark

yes you can

  1. Build data table with two columns key and value.
  2. have a loop for dictionary
  3. for each item add data row (output of build data table) input {item.key, item.value}
3 Likes

Hey @charliefik

you can follow @Divyashreem steps.
just adding one point for more clarification.

Use a for each loop with argument
System.Collections.Generic.KeyValuePair<System.String,System.String)

If still having doubt you can look into attached old sample - Convert_Dict_To_Datatable.xaml (10.1 KB)

Regards…!!
Aksh

1 Like

Thanks very much @Divyashreem and @aksh1yadav. I just looped through the dictionary and wrote directly into the excel using write cell (rather than convert to datatable first). I was hoping that there was some kind of instant conversion function like Cint (for example turning a string into an integer) that could be applied to a dictionary to turn it into a datatable then to excel via write range.(just being lazy!)

As you say @aksh1yadav making sure that you are using the correct argument type in the for each loop is essential and good job adding an example its very helpful.

All the best,