-
Notifications
You must be signed in to change notification settings - Fork 297
LoadFromDictionaries
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 is available from version 5.2.1 and higher.
The main purpose of this method is to support IEnumerables of ExpandoObjects, 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's:
dynamic o1 = new ExpandoObject();
o1.Id = 1;
o1.Name = "TestName 1";
dynamic o2 = new ExpandoObject();
o2.Id = 2;
o2.Name = "TestName 2";
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);
}By supplying the parameter PrintHeaders to the LoadFromDictionaries method you can add headers to the first row above the data. EPPlus will use the property names/Dictionary keys as header names.
sheet.Cells["A1"].LoadFromDictionaries(items, c => c.PrintHeaders = true);If you supply the parameter TableStyle EPPlus will create a table for the data in the worksheet. The TableStyles enum contains over 60 different table styles to choose from.
sheet.Cells["A1"].LoadFromDictionaries(items, true, TableStyles.Dark1);
// alternatively
sheet.Cells["A1"].LoadFromDictionaries(items, c =>
{
c.PrintHeaders = true;
c.TableStyle = TableStyles.Dark1;
});If you supply the Keys argument only properties/values matching those keys will be included.
using (var package = new ExcelPackage())
{
var sheet = package.Workbook.Worksheets.Add("test");
sheet.Cells["A1"].LoadFromDictionaries(_items, false, TableStyles.None, new string[] { "Name" });
// alternatively
sheet.Cells["A1"].LoadFromDictionaries(items, c =>
{
c.Keys = new string[] { "Name" }
});
}EPPlus Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Breaking Changes in EPPlus 8
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Hyperlinks
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- The ExcelRange.Text property
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles