How to write dictionary object in excel

Hello,


We are using Aspose.Cells for our export to excel requirements.There are different import object methods to write data in excel. Previously i was using following method where i had only one List object.

List tempreportList = new List();
worksheet.Cells.ImportCustomObjects((System.Collections.ICollection)tempreportList, 0, 0, null);

but now our requirement has been changed and we now creating dynamic class with dynamic properties and values. so now i have one dictionary object as follows.

var test = new Dictionary<string, List>();

In which keys are the header of the list and List is the values which should be write under that column.

so our test object will look like as follows.

test = {“Name”, [“asley”,“roger”,“test1”]}
{“Number”, [1,2,3]}
{“Date”, []}
{“Currency”, [10.5,11.2,20.3]}

So Can anyone help me in writing this dictionary object to excel? Is there any inbuilt method which aspose provides to write this? it would be appreciated if someone helps me to write this complex logic to export in excel

Thanks & Regards
Ashish Rajguru

Hi Ashish,


Thank you for contacting Aspose support.

I am afraid, Aspose.Cells APIs do not provide direct means/methods to import Dictionary objects to worksheet cells. However, you can iterate over the Dictionary items and insert individual cell values as demonstrated below.

C#

var book = new Workbook();
var cells = book.Worksheets[0].Cells;
int col = 0;
int row = 0;

var dictionary = new Dictionary<string, List<object>>();
dictionary.Add(“Names”, new List<object> { “John”, “billy”, “Adam” });
dictionary.Add(“Dates”, new List<object> { DateTime.Parse(“2000-02-02”), DateTime.Parse(“2010-02-02”), DateTime.Parse(“2010-03-02”) });
dictionary.Add(“Numbers”, new List<object> { 12, 12, 13 });

foreach (KeyValuePair<string, List<object>> entry in dictionary)
{
Console.WriteLine(entry.Key);
cells[row, col].PutValue(entry.Key);
foreach (var value in entry.Value)
{
Console.WriteLine(value);
row++;
cells[row, col].PutValue(value.ToString());
}
row = 0;
col++;
}

book.Save(“D:/output.xlsx”, SaveFormat.Xlsx);