Skip to content

Customizations

Jonathan Paré edited this page Feb 27, 2017 · 12 revisions

Many customization can applied using the fluent interface.

Display

Display is used to control visibility as well as the visual order of a specific property. Basically, if you need to quickly reorder the columns or only take a few of them, Display is what you are searching for. If this function is not called, all the public properties of the object will be exported to Excel.

Example:

var data = new[]
{
	new { TextValue = "SomeText", DateValue = DateTime.Now, DoubleValue = 10.2, IntValue = 5}
};
var excelWorksheet1 = EnumerableExporter.Create(data).CreateExcelPackage();

//Output: TextValue, DateValue, DoubleValue, IntValue
var excelWorksheet2 = EnumerableExporter.Create(data)
	.Display(n => new
	{
		n.DoubleValue, //first column
		n.TextValue    //second column
	})
	.CreateExcelPackage();

//Output: DoubleValue, TextValue

Ignore

Used to skip/ignore one or many columns when generating the document. Ignore will have precedence over the Display function.

Example:

var data = new[]
{
	new { TextValue = "SomeText", DateValue = DateTime.Now, DoubleValue = 10.2, IntValue = 5}
};

var excelWorksheet = EnumerableExporter.Create(data)
	.Ignore(n => new
	{
		n.TextValue,
		n.DoubleValue
	})
	.CreateExcelPackage();

//Output: DateValue, IntValue

NumberFormatFor

Used to set a specific format (just like you would using Excel)

Example:

var data = new[]
{
	new { TextValue = "SomeText", DateValue = DateTime.Now, DoubleValue = 10.2, IntValue = 5}
};

var excelWorksheet = EnumerableExporter.Create(data)
	.NumberFormatFor(n => n.DateValue, "yyyy-MM-dd HH:mm")
	.NumberFormatFor(n => n.DoubleValue, "0.00 $")
	.NumberFormatFor(n => n.IntValue, "00")
	.CreateExcelPackage();

TextFormatFor

Used to convert a value to text.

StyleFor

Used to alter the style of a specific column.

HeaderStyleFor

Used to alter the header style of a specific column.

CustomizeTable

DefaultNumberFormat

Used to specify a default display format for a specific type.

ConditionalStyleFor

...

Quick customization can be accomplished by using the fluent interface like this:

var excelPackage = EnumerableExporter.Create(employees)
	.DefaultNumberFormat(typeof(DateTime), "yyyy-MM-dd") //set a default format for all DateTime columns
	.NumberFormatFor(n => n.DateOfBirth, "{0:yyyy-MMM-dd}") //set a specific format for the "DateOfBirth"
	.Ignore(n => new { n.UserName, n.Email }) //remove 2 columns from the output
	.TextFormatFor(n => n.Phone, "Cell: {0}") //add a prefix to the value
	.StyleFor(n => n.DateContractEnd, style =>
	{
	    style.Fill.Gradient.Color1.SetColor(Color.Yellow);
	    style.Fill.Gradient.Color2.SetColor(Color.Green);
	}) //the cells in this columns now have a gradiant background
	.CreateExcelPackage();