forked from rigofunc/NPOI.Extension
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathFluentConfigurationExtensions.cs
47 lines (42 loc) · 1.49 KB
/
FluentConfigurationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using FluentExcel;
namespace samples
{
public static class FluentConfigurationExtensions
{
public static FluentConfiguration<TModel> FromAnnotations<TModel>(this FluentConfiguration<TModel> fluentConfiguration) where TModel : class
{
var properties = typeof(TModel).GetProperties();
foreach (var property in properties)
{
var pc = fluentConfiguration.Property(property);
var display = property.GetCustomAttribute<DisplayAttribute>();
if (display != null)
{
pc.HasExcelTitle(display.Name);
if (display.GetOrder().HasValue)
{
pc.HasExcelIndex(display.Order);
}
}
else
{
pc.HasExcelTitle(property.Name);
}
var format = property.GetCustomAttribute<DisplayFormatAttribute>();
if (format != null)
{
pc.HasDataFormatter(format.DataFormatString
.Replace("{0:", "")
.Replace("}", ""));
}
if (pc.Index < 0)
{
pc.HasAutoIndex();
}
}
return fluentConfiguration;
}
}
}