Kevull.EPPLus.MultiHeader
2.1.0
Prefix Reserved
dotnet add package Kevull.EPPLus.MultiHeader --version 2.1.0
NuGet\Install-Package Kevull.EPPLus.MultiHeader -Version 2.1.0
<PackageReference Include="Kevull.EPPLus.MultiHeader" Version="2.1.0" />
<PackageVersion Include="Kevull.EPPLus.MultiHeader" Version="2.1.0" />
<PackageReference Include="Kevull.EPPLus.MultiHeader" />
paket add Kevull.EPPLus.MultiHeader --version 2.1.0
#r "nuget: Kevull.EPPLus.MultiHeader, 2.1.0"
#:package Kevull.EPPLus.MultiHeader@2.1.0
#addin nuget:?package=Kevull.EPPLus.MultiHeader&version=2.1.0
#tool nuget:?package=Kevull.EPPLus.MultiHeader&version=2.1.0
Kevull.EPPLus.MultiHeader
Extension for the EPPlus library to create reports from complex objects
Given a list like this:
var complexObject = new List<RootLevel> {
new RootLevel {
SimpleProperty = "String1",
ComplexProperty = new SecondLevel
{
LeftColumn = "Left side 1",
RightColumn = new ThirdLevel
{
CatA = 11,
CatB = 12,
CatC = 13
}
}
},
new RootLevel {
SimpleProperty = "String2",
ComplexProperty = new SecondLevel
{
LeftColumn = "Left side 2",
RightColumn = new ThirdLevel
{
CatA = 21,
CatB = 22,
CatC = 23
}
}
}
};
this code:
using var xls = new ExcelPackage();
var report = new MultiHeaderReport<RootLevel>(xls, "Object");
report.GenerateReport(complexObject);
xls.SaveAs("Report.xlsx");
will render like this:
Columns customization
The example above will apply default format. But it's possible to customize columns or add calculated ones. The general syntax configuration is like this
using var xls = new ExcelPackage();
List<Person> people = GetPeople();
var report = new MultiHeaderReport<Person>(xls, "People");
report.Configure(options => options
.AddNamedStyle("CompleteNameStye", s => {
s.Font.Italic = true;
})
.AddColumn(x => x.Name, 1)
.AddColumn(x => x.Surname, 2)
.AddFormula("CompleteName", "CONCATENATE(B2,\", \",A2)", 3, styleName: "CompleteNameStye")
.IgnoreColumn(x => x.BirthDay)
).GenerateReport(people);
xls.SaveAs(path);
Columns
By default each leaf property in the object is rendered into a Column. There are a set of column types depending on the property data type and your necessities. The generic method is:
AddColumn(Expression<Func<T, object?>> columnSelector, int? order = null, string? displayName = null, bool hidden = false, string? styleName = null)
- columnSelector: allows to specify at compilation time the property to be rendered
- order: Diplay order (1-based). Order is relative to the other columns. Columns that have no order are added after those that have it
- displayName: Human friendly name for the column to be shown in Excel. If not specified, the property Name is used.
- hidden: Column is written to Excel, but it's hidden. Don't confuse with
IgnoreColumn
method. - styleName: style name of any style defined in Workbook.Styles. See below
AddEnumeration
Specialized column that renders data from a IDictionary<TKey, TValue>
or IEnumerable<T>
This column will render always two header levels:
- The first with the property name
- The second with the possible values in the collection
It's important to know in advance the number of columns to be rendered. For that you need to specify the keyValues
parameter. If, during the report generation a value other that the specified is found, an exception it's thrown.
For example, given a list of objects of type:
class Product {
public property Name {get; set; }
public Dictionary<string, int> Categories {get; private set; } = new Dictionary<string, int> {
{ "CatA", 1 },
{ "CatB", 2 },
{ "CatC", 3 }
}
}
you can configure the column as follow:
List<Product> data = GetProducts();
var report = new MultiHeaderReport<Product>(xls, "Object");
report.Configure(options =>
options.AddEnumeration(x => x.Categories, data.First().Categories.Keys)
);
AddExpression
Allows to execute a lambda function for each row and return a custom value, for example
report.Configure(options =>
options.AddExpression("Initials", x => string.Concat(x.Name[0], '.', x.Surname[0], '.'))
);
AddFormula
Each time the report renders a value for this column, it will use the specified formula. Be sure to use the correct absulte/relative references in the formula. You must specify the formula without the usual starting = symbol
report.Configure(options => options
.AddColumn(x => x.Name, 1)
.AddColumn(x => x.Surname, 2)
.AddFormula("CompleteName", "CONCATENATE(B2,\", \",A2)", 3)
AddHyperLinkColumn
Adds a column whose cells will contain a hyperlink. This involves two properties. For example:
var report = new MultiHeaderReport<Person>(xls, "People");
report.Configure(options => options
.AddHyperLinkColumn(x => x.Name, x => x.Profile)
.IgnoreColumn(x => x.Profile)
).GenerateReport(people);
This will create a column titled Name with an hyperlink to the url of the person's profile. As the Profile property is already used in the Name column, it's convenient to ignore it (do not render the property itself).
IgnoreColumn
This column will not be rendered. Do not confuse with the hidden pameter in the AddXXXXX methods. With hidden, the column is rendered to an Excel column, but the column is hidden. In this case the property's data doesn't exist in the resulting Excel.
Set column width
Allows to configure the column's width behaviour: Default, Custom, Auto, Hidden
var report = new MultiHeaderReport<Person>(xls, "People");
report.Configure(options => options
.AddColumn(x => x.Name, cfg =>
cfg.ColumnWidth.SetWidth(WidthType.Auto))
.AddColumn(x => x.Surname, cfg =>
cfg.ColumnWidth.SetWidth(8.0))
).GenerateReport(people);
Styles
AddNamedStyle
AddNamedStyle(string name, Action<ExcelStyle> style)
It's possible to create an Excel style and then apply it to any number of columns. AddNamedStyle
is a wrapper to
the Workbook.Styles.CreateNamedStyle()
EPPlus method. You can either prebuild a named style first with EPPlus and then assign the style name, or use AddNamedStyle.
When you assign a styleName to a column the name is not verified in the configuration phase, but during the report generation.
AddHeaderStyle
AddNamedStyle(Action<ExcelStyle> style)
Similar to the previous one, but specific to apply format to the headers. If you do not provide any format to the headers, a default one will be used.
Default style for Date or Time columns
DateTime, DateOnly and TimeOnly properties are rendered with the corresponding default Date or Time format. You can override the default formats with AddNamedStyle
and apply the built style to the column definition.
Other customizations
AppendToExistingReport
Setting to true it will append data below the last existing data row. It's assumed that the report configuration will be the same that when the previous report was generated.
Autofilter
If true, the report will activate the Excel autofilter for you.
SetStartingAddress
Default top-left cell is A1. With SetStartingAddress
you can specify any other starting cell.
Roadmap
- AutoFilter ✓
- Columns width: Default, specific width, Auto ✓
- Conditional format
- Append rows to an existing report ✓
- Posibility to change the top-left starting point ✓
- Target netstandard 2.0 in nuget package ✓
- Frozen rows and columns ✓
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
2.1.0 | 105 | 7/12/2025 |
2.0.0 | 246 | 11/29/2024 |
1.1.0 | 165 | 9/28/2024 |
1.0.0 | 166 | 8/22/2024 |
1.0.0-beta5 | 94 | 8/4/2024 |