Skip to content

LoadFromDictionaries

AdrianEPPlus edited this page Sep 2, 2024 · 18 revisions

This method loads data from a collection of IDictionary<string, object> or ExpandoObject (which implements the IDictionary<string, object> interface) into a spreadsheet.

The LoadFromDictionaries method is available from version 5.2.1 and higher. You can see this method in action in our Blazor sample

Basic usage

The main purpose of this method is to support IEnumerables<ExpandoObject>, so that it what we will use in these examples. But you can also create your own dictionaries to load the data.

First, let us create a list of System.Dynamic.ExpandoObject:

dynamic o1 = new ExpandoObject();
o1.Id = 1;
o1.Name = "TestName 1";
o1.Number = 10;
dynamic o2 = new ExpandoObject();
o2.Id = 2;
o2.Name = "TestName 2";
o2.Number = 20;
var items = new List<ExpandoObject>()
{
    o1,
    o2
};

Now we can import these objects to a spreadsheet using the LoadFromDictionaries method.

using (var package = new ExcelPackage())
{
    var sheet = package.Workbook.Worksheets.Add("test");
    var r = sheet.Cells["A1"].LoadFromDictionaries(items);
}

Headers