-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathToCollectionSample.vb
More file actions
126 lines (100 loc) · 7.52 KB
/
ToCollectionSample.vb
File metadata and controls
126 lines (100 loc) · 7.52 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
Imports OfficeOpenXml
Imports OfficeOpenXml.Table
Imports System
Namespace EPPlusSamples
Public Module ToCollectionSample
Public Sub Run()
Console.WriteLine("Running sample 2.7 - ToCollection and ToCollectionWithMappings")
Using package = New ExcelPackage()
Dim ws = package.Workbook.Worksheets.Add("Persons")
' Load the sample data into the worksheet
Dim range = ws.Cells("A1").LoadFromCollection(Persons, Sub(options)
options.PrintHeaders = True
options.TableStyle = TableStyles.Dark1
End Sub)
' ********************************************************
' ToCollection. Automaps cell data to class instance *
' ********************************************************
Console.WriteLine("******* Sample 2.2 - ToCollection ********" & vbLf)
' export the data loaded into the worksheet above to a collection
Dim exportedPersons = range.ToCollection(Of ToCollectionSamplePerson)()
For Each person In exportedPersons
Console.WriteLine("***************************")
Console.WriteLine($"Name: {person.FirstName} {person.LastName}")
Console.WriteLine($"Height: {person.Height} cm")
Console.WriteLine($"Birthdate: {person.BirthDate.ToShortDateString()}")
Next
Console.WriteLine()
' ********************************************************
' ToCollectionWithMappings. Use this method to manually *
' map all or just some of the cells to your class. *
' ********************************************************
Console.WriteLine("******* Sample 2.2 - ToCollectionWithMappings ********" & vbLf)
Dim exportedPersons2 = ws.Cells("A1:D4").ToCollectionWithMappings(Function(row)
' this runs once per row in the range
' Create an instance of the exported class
Dim person = New ToCollectionSamplePerson()
' If some of the cells can be automapped, start by automapping the row data to the class
row.Automap(person)
' Note that you can only use column names as below
' if options.HeaderRow is set to the 0-based row index
' of the header row.
person.FirstName = row.GetValue(Of String)("FirstName")
' get value by the 0-based column index
person.Height = row.GetValue(Of Integer)(2)
' return the class instance
Return person
End Function, Sub(options) options.HeaderRow = 0)
For Each person In exportedPersons2
Console.WriteLine("***************************")
Console.WriteLine($"Name: {person.FirstName} {person.LastName}")
Console.WriteLine($"Height: {person.Height} cm")
Console.WriteLine($"Birthdate: {person.BirthDate.ToShortDateString()}")
Next
Console.WriteLine()
' ********************************************************
' ToCollection. Using property attributes for mappings, *
' see the ToCollectionSamplePersonAttr class *
' ********************************************************
' Load the sample data into a new worksheet
Dim ws2 = package.Workbook.Worksheets.Add("Ws2")
Dim range2 = ws2.Cells("A1").LoadFromCollection(PersonsWithAttributes, Sub(options)
options.PrintHeaders = True
options.TableStyle = TableStyles.Dark1
End Sub)
Console.WriteLine("******* Sample 2.2 - ToCollection using attributes ********" & vbLf)
' export the data loaded into the worksheet above to a collection
Dim exportedPersons3 = range2.ToCollection(Of ToCollectionSamplePersonAttr)()
For Each person In exportedPersons3
Console.WriteLine("***************************")
Console.WriteLine($"Name: {person.FirstName} {person.LastName}")
Console.WriteLine($"Height: {person.Height} cm")
Console.WriteLine($"Birthdate: {person.BirthDate.ToShortDateString()}")
Next
Console.WriteLine()
' ********************************************************
' ToCollection from a table *
' ********************************************************
Console.WriteLine("******* Sample 2.2 - ToCollection from a table ********" & vbLf)
' Load the sample data a new worksheet
Dim ws3 = package.Workbook.Worksheets.Add("Ws3")
Dim tableRange = ws3.Cells("A1").LoadFromCollection(Persons, Sub(options)
options.PrintHeaders = True
options.TableStyle = TableStyles.Dark1
End Sub)
Dim table = ws3.Tables.GetFromRange(tableRange)
' export the data loaded into the worksheet above to a collection
Dim exportedPersons4 = table.ToCollection(Of ToCollectionSamplePerson)()
For Each person In exportedPersons4
Console.WriteLine("***************************")
Console.WriteLine($"Name: {person.FirstName} {person.LastName}")
Console.WriteLine($"Height: {person.Height} cm")
Console.WriteLine($"Birthdate: {person.BirthDate.ToShortDateString()}")
Next
Console.WriteLine()
Console.WriteLine("Sample 2.7 finished.")
Console.WriteLine()
End Using
End Sub
End Module
End Namespace