How do I print from Dictionary to Excel?

Hello All, my first post here.

I am curious how to print my data from a dictionary to an Excel file.
The dictionary (myDict) contains a key (string) and a List(of string).
The List(of string) is called dictList and contains two items.
I initialize both at the beginning to be able to work with them.
Then I read the range of my config xlsx file to add the key-value pairs to the dictionary and they correctly appear in the dictionary.
Example: “180012”: {“Eastern Airlines”, “Aviation”}

Now, I would like to print the content to Excel by using Build Data Table activity with output outDT and later use Write Range (with outDT as input), all in a For Each “key” in “myDict” activity.

I am struggling how to achieve this given that the dictionary has a list as its values and i need to print them in seperate cells. Output should look like in Excel: A1: key, B1: value(0), C1: value(1). Thanks!

1 Like

Hi @Uhrinowitz

Welcome to UiPath community
hope this could help you

Cheers

Hi @Uhrinowitz,
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}

Thanks for both for the quick reply! I think I’m on the right track but something else it missing.
During the add data row, the input (ArrayRow), {item.key, item.value} seems to be not working. When I type “item.” … the “key” wont appear at all so I am guessing that this has something to do with the data typy settings? Please see screenshot below.

For each:
image

Hi @Uhrinowitz

Give type argument as string and pass item into data row

Thanks
Ashwin.S

Is your type argument is of this type?

You can access the key as item.key bro…

The variable type is
image
So, the key is a string and the value is a list of string.

I will try, thanks!

So, I changed the For Each loop argument type to KeyValuePair(String, List(Of String)) and it seems to work!

For testing purposes, I used a Message Box with “item.Key + " - " + item.Value(i) + " + " + item.Value(i+1)”
However, it is a bit weird because initially I wanted to use item.value(0) and item.value(1) to dispaly both values of the dictionary values list. This turned out to be a fail because it seems that it adds the whole list to the dictionary so I suppose i need to clear the list after adding it to the dictionary. I will come back :slight_smile:

Yes bro, do it and let me know

1 Like

Guys, thanks for all the valuable input! Every solution is a bit different so this one is like the mix of all the other topics your guys linked.

Adding a list to a dictionary and writing it to Excel

Creating the dictionary

  1. Initialize dictionary of type IDictionary(String, List(Of String))
    Use Assign activity and New Dictionary(Of String, List(Of String))()
  2. A List(Of String) variable had to be initialized
  3. The List(Of String) was added to the Value field of the Add to dictionary activity in a For each loop.
    Note: You need to re-initialize the list after adding it to the dictionay else it will keep adding all the elements to it!

Writing dictionary to Excel

  1. Build Data Table activity to set up the DT you want to use
  2. Use a For each loop with Type Argument of KeyValuePair(String, List(Of String)) - this is crucial!
  3. Inside the body of the loop, palce an Add data row activity
  4. In the Input Pane, type {item.Key, item.Value(0), item.Value(1)} in ArrayRow (as we are working with a list!). Note: If “key” will not pop up after you type “item.” then you are using incorrect TypeArgument.
  5. Add an Excel Application Scope and a Write Range to write the Data Table you specified in the Build Data Table activity!
2 Likes

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