Add Dictionary key Value to data table

I have variables CompanyName,Expenditure,Total Value variables in the dictionary of the format New Dictionary(Of String, Tuple(Of String, String)) From {{“ABC”, New Tuple(Of String, String)(“1236”,“6786”)}} and so on…i want my output in excel as

(Here Expenditure and total is the main header under which the column company is the sub header)

@tazunnisa.badavide

Create your dictionary as you’ve mentioned.

Use Build Data Table Create an empty DataTable with columns “Company”, “Expenditure”, and “Total”.

For Each (Key-Value) in Dictionary: Iterate through the dictionary.

Add Data Row: Add a row to the DataTable for each company.
Company: Key
Expenditure: Value.Item1
Total: Value.Item2
Write the DataTable to an Excel file.

Cheers…!

Hey @tazunnisa.badavide ,

Kindly try the below code


Try
Dim dictionaryValues = New Dictionary(Of String, Tuple(Of String, String)) From {{"1", New Tuple(Of String, String)("123","678")}}



dt.Columns.Add("Expenditure")
dt.Columns.Add("Total")
For Each kvp As KeyValuePair(Of String, Tuple(Of String, String)) In dictionaryValues
   Dim dataRow As DataRow = dt.NewRow()
   dataRow("Expenditure") = kvp.Key
   dataRow("Total") = kvp.Value.Item1
   dt.Rows.Add(dataRow)
Next



Catch
Catch ex As Exception
  console.WriteLine(ex)
End Try

Hope it helps you !

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