-
Notifications
You must be signed in to change notification settings - Fork 16
/
EnumerableExporter.cs
215 lines (182 loc) · 8.09 KB
/
EnumerableExporter.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Collections.Generic;
using System.Linq;
using Dexiom.EPPlusExporter.Helpers;
using OfficeOpenXml;
using OfficeOpenXml.Table;
namespace Dexiom.EPPlusExporter
{
#region Create Method (using type inference)
public static class EnumerableExporter
{
public static EnumerableExporter<T> Create<T>(IEnumerable<T> data, TableStyles tableStyles = TableStyles.Light1) where T : class => new EnumerableExporter<T>(data) { TableStyle = tableStyles };
public static EnumerableExporter<T> Create<T>(IEnumerable<T> data, IEnumerable<DynamicProperty<T>> dynamicProperties, TableStyles tableStyles = TableStyles.Light1) where T : class => new EnumerableExporter<T>(data, dynamicProperties) { TableStyle = tableStyles };
}
#endregion
public class EnumerableExporter<T> : TableExporter<T>
where T : class
{
public IEnumerable<T> Data { get; set; }
public IEnumerable<DynamicProperty<T>> DynamicProperties { get; set; }
#region Constructors
public EnumerableExporter(IEnumerable<T> data)
{
Data = data;
}
public EnumerableExporter(IEnumerable<T> data, IEnumerable<DynamicProperty<T>> dynamicProperties)
{
Data = data;
DynamicProperties = dynamicProperties;
}
#endregion
#region Protected
protected override ExcelRange AddWorksheet(ExcelPackage package)
{
const int headerFirstRow = 1;
const int headerFirstCol = 1;
const int dataFirstRow = 2;
const int dataFirstCol = 1;
if (Data == null)
return null;
//let's avoid multiple enumeration
var data = Data as IList<T> ?? Data.ToList();
IList<DynamicProperty<T>> dynamicProperties = null;
if (DynamicProperties != null)
dynamicProperties = DynamicProperties as IList<DynamicProperty<T>> ?? DynamicProperties.ToList();
//get available properties
var properties = ReflectionHelper.GetBaseTypeOfEnumerable(data).GetProperties()
.Where(p => !IgnoredProperties.Contains(p.Name))
.ToList();
//resolve displayed properties
HashSet<string> allPropertyNames;
if (DisplayedProperties != null)
{
allPropertyNames = DisplayedProperties;
}
else
{
allPropertyNames = new HashSet<string>(properties.Select(n => n.Name));
if (dynamicProperties != null)
{
foreach (var dynamicPropertyName in dynamicProperties.Select(n => n.Name))
allPropertyNames.Add(dynamicPropertyName);
}
}
var displayFields = new List<DisplayField<T>>();
foreach (var propertyName in allPropertyNames)
{
var property = properties.FirstOrDefault(n => n.Name == propertyName);
if (property != null)
{
//displayedProperties.Add(property); //todo: delete me
displayFields.Add(new DisplayField<T>(property));
}
else
{
var dynamicProperty = DynamicProperties?.FirstOrDefault(n => n.Name == propertyName);
if (dynamicProperty != null)
displayFields.Add(new DisplayField<T>(dynamicProperty));
}
}
//init the configurations
var columnConfigurations = GetColumnConfigurations(displayFields.Select(n => n.Name));
//create the worksheet
var worksheet = package.Workbook.Worksheets.Add(WorksheetName);
//Create table header
{
var col = headerFirstCol;
foreach (var displayField in displayFields)
{
var colConfig = columnConfigurations[displayField.Name];
var cell = worksheet.Cells[headerFirstRow, col];
cell.Value = string.IsNullOrEmpty(colConfig.Header.Text) ? displayField.DisplayName : colConfig.Header.Text;
colConfig.Header.SetStyle(cell.Style);
col++;
}
}
//Add rows
var row = dataFirstRow;
foreach (var item in data)
{
var iCol = dataFirstCol;
foreach (var displayField in displayFields)
{
var cell = worksheet.Cells[row, iCol];
cell.Value = ApplyTextFormat(displayField.Name, displayField.GetValue(item));
iCol++;
}
row++;
}
//get bottom & right bounds
var dataLastCol = dataFirstCol + displayFields.Count - 1;
var dataLastRow = dataFirstRow + Math.Max(data.Count, 1) - 1; //make sure to have at least 1 data line (for table format)
var tableRange = worksheet.Cells[headerFirstRow, headerFirstCol, dataLastRow, dataLastCol];
WorksheetHelper.FormatAsTable(tableRange, TableStyle, WorksheetName, false);
//apply configurations
{
var iCol = dataFirstCol;
foreach (var displayField in displayFields)
{
var colConfig = columnConfigurations[displayField.Name];
var columnRange = worksheet.Cells[dataFirstRow, iCol, dataLastRow, iCol];
//apply default number format
if (DefaultNumberFormats.ContainsKey(displayField.Type))
columnRange.Style.Numberformat.Format = DefaultNumberFormats[displayField.Type];
//apply number format
if (colConfig.Content.NumberFormat != null)
columnRange.Style.Numberformat.Format = colConfig.Content.NumberFormat;
//apply style
colConfig.Content.SetStyle(columnRange.Style);
if (colConfig.Width.HasValue)
worksheet.Column(iCol).Width = colConfig.Width.Value;
else if (AutoFitColumns)
worksheet.Column(iCol).AutoFit();
iCol++;
}
}
//apply conditional styles
{
var iCol = dataFirstCol;
foreach (var displayField in displayFields)
{
if (ConditionalStyles.ContainsKey(displayField.Name))
{
var conditionalStyle = ConditionalStyles[displayField.Name];
var iRow = dataFirstRow;
foreach (var item in data)
{
var cell = worksheet.Cells[iRow, iCol];
conditionalStyle(item, cell.Style); //apply style on cell
iRow++;
}
}
iCol++;
}
}
//apply conditional styles
{
var iCol = dataFirstCol;
foreach (var displayField in displayFields)
{
if (Formulas.ContainsKey(displayField.Name))
{
var formulaFormat = Formulas[displayField.Name];
var iRow = dataFirstRow;
foreach (var item in data)
{
var cell = worksheet.Cells[iRow, iCol];
var formula = formulaFormat(item, cell.Value); //apply style on cell
cell.Value = null;
cell.Formula = formula;
iRow++;
}
}
iCol++;
}
}
//return the entire grid range
return tableRange;
}
#endregion
}
}