-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLoadingDataFromCollectionWithAttributes.vb
More file actions
172 lines (152 loc) · 6.35 KB
/
LoadingDataFromCollectionWithAttributes.vb
File metadata and controls
172 lines (152 loc) · 6.35 KB
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
Imports OfficeOpenXml
Imports OfficeOpenXml.Attributes
Imports OfficeOpenXml.Table
Imports System
Imports System.Collections.Generic
Namespace EPPlusSamples.LoadingData
<EpplusTable(TableStyle:=TableStyles.Dark1, PrintHeaders:=True, AutofitColumns:=True, AutoCalculate:=False, ShowTotal:=True, ShowFirstColumn:=True)>
<EpplusFormulaTableColumn(Order:=6, NumberFormat:="€#,##0.00", Header:="Tax amount", FormulaR1C1:="RC[-2] * RC[-1]", TotalsRowFunction:=RowFunctions.Sum, TotalsRowNumberFormat:="€#,##0.00"), EpplusFormulaTableColumn(Order:=7, NumberFormat:="€#,##0.00", Header:="Net salary", Formula:="E2-G2", TotalsRowFunction:=RowFunctions.Sum, TotalsRowNumberFormat:="€#,##0.00")>
Friend Class Actor
<EpplusIgnore>
Public Property Id As Integer
<EpplusTableColumn(Order:=3)>
Public Property LastName As String
<EpplusTableColumn(Order:=1, Header:="First name")>
Public Property FirstName As String
<EpplusTableColumn(Order:=2)>
Public Property MiddleName As String
<EpplusTableColumn(Order:=0, NumberFormat:="yyyy-MM-dd", TotalsRowLabel:="Total")>
Public Property Birthdate As Date
<EpplusTableColumn(Order:=4, NumberFormat:="€#,##0.00", TotalsRowFunction:=RowFunctions.Sum, TotalsRowNumberFormat:="€#,##0.00")>
Public Property Salary As Double
<EpplusTableColumn(Order:=5, NumberFormat:="0%", TotalsRowFormula:="Table1[[#Totals],[Tax amount]]/Table1[[#Totals],[Salary]]", TotalsRowNumberFormat:="0 %")>
Public Property Tax As Double
End Class
<EpplusTable(TableStyle:=TableStyles.Medium1, PrintHeaders:=True, AutofitColumns:=True, AutoCalculate:=True, ShowLastColumn:=True)>
Friend Class Actor2
Inherits Actor
End Class
' classes used to demonstrate this functionality with a complex type property
<EpplusTable(TableStyle:=TableStyles.Light14, PrintHeaders:=True, AutofitColumns:=True, AutoCalculate:=True, ShowLastColumn:=True)>
Friend Class Actor3
<EpplusIgnore>
Public Property Id As Integer
<EpplusNestedTableColumn(Order:=1)>
Public Property Name As ActorName
<EpplusTableColumn(Order:=0, NumberFormat:="yyyy-MM-dd", TotalsRowLabel:="Total")>
Public Property Birthdate As Date
<EpplusTableColumn(Order:=2, NumberFormat:="€#,##0.00", TotalsRowFunction:=RowFunctions.Sum, TotalsRowNumberFormat:="€#,##0.00")>
Public Property Salary As Double
<EpplusTableColumn(Order:=3, NumberFormat:="0%", TotalsRowFormula:="Table1[[#Totals],[Tax amount]]/Table1[[#Totals],[Salary]]", TotalsRowNumberFormat:="0 %")>
Public Property Tax As Double
End Class
Friend Class ActorName
<EpplusTableColumn(Order:=3)>
Public Property LastName As String
<EpplusTableColumn(Order:=1, Header:="First name")>
Public Property FirstName As String
<EpplusTableColumn(Order:=2)>
Public Property MiddleName As String
End Class
Public Module LoadingDataFromCollectionWithAttributes
Public Sub Run()
' sample data
Dim actors = New List(Of Actor) From {
New Actor With {
.Salary = 256.24,
.Tax = 0.21,
.FirstName = "John",
.MiddleName = "Bernhard",
.LastName = "Doe",
.Birthdate = New DateTime(1950, 3, 15)
},
New Actor With {
.Salary = 278.55,
.Tax = 0.23,
.FirstName = "Sven",
.MiddleName = "Bertil",
.LastName = "Svensson",
.Birthdate = New DateTime(1962, 6, 10)
},
New Actor With {
.Salary = 315.34,
.Tax = 0.28,
.FirstName = "Lisa",
.MiddleName = "Maria",
.LastName = "Gonzales",
.Birthdate = New DateTime(1971, 10, 2)
}
}
Dim subclassActors = New List(Of Actor2) From {
New Actor2 With {
.Salary = 256.24,
.Tax = 0.21,
.FirstName = "John",
.MiddleName = "Bernhard",
.LastName = "Doe",
.Birthdate = New DateTime(1950, 3, 15)
},
New Actor2 With {
.Salary = 278.55,
.Tax = 0.23,
.FirstName = "Sven",
.MiddleName = "Bertil",
.LastName = "Svensson",
.Birthdate = New DateTime(1962, 6, 10)
},
New Actor2 With {
.Salary = 315.34,
.Tax = 0.28,
.FirstName = "Lisa",
.MiddleName = "Maria",
.LastName = "Gonzales",
.Birthdate = New DateTime(1971, 10, 2)
}
}
Dim complexTypeActors = New List(Of Actor3) From {
New Actor3 With {
.Salary = 256.24,
.Tax = 0.21,
.Name = New ActorName With {
.FirstName = "John",
.MiddleName = "Bernhard",
.LastName = "Doe"
},
.Birthdate = New DateTime(1950, 3, 15)
},
New Actor3 With {
.Salary = 278.55,
.Tax = 0.23,
.Name = New ActorName With {
.FirstName = "Sven",
.MiddleName = "Bertil",
.LastName = "Svensson"
},
.Birthdate = New DateTime(1962, 6, 10)
},
New Actor3 With {
.Salary = 315.34,
.Tax = 0.28,
.Name = New ActorName With {
.FirstName = "Lisa",
.MiddleName = "Maria",
.LastName = "Gonzales"
},
.Birthdate = New DateTime(1971, 10, 2)
}
}
Using package = New ExcelPackage(FileUtil.GetCleanFileInfo("2.1-LoadFromCollectionAttributes.xlsx"))
' using the Actor class above
Dim sheet = package.Workbook.Worksheets.Add("Actors")
sheet.Cells("A1").LoadFromCollection(actors)
' using a subclass where we have overridden the EpplusTableAttribute (different TableStyle and highlight last column instead of the first).
Dim subclassSheet = package.Workbook.Worksheets.Add("Using subclass with attributes")
subclassSheet.Cells("A1").LoadFromCollection(subclassActors)
' using a subclass where we have overridden the EpplusTableAttribute (different TableStyle and highlight last column instead of the first).
Dim complexTypePropertySheet = package.Workbook.Worksheets.Add("Complex type property")
complexTypePropertySheet.Cells("A1").LoadFromCollection(complexTypeActors)
package.Save()
End Using
End Sub
End Module
End Namespace