-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadCsv.vb
63 lines (60 loc) · 2.08 KB
/
readCsv.vb
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
''' <summary>
''' a class handling csv file
''' </summary>
''' <remarks></remarks>
Public Class Csv
Dim filepath As String
Dim encoding As Encoding
Dim headerRow As Boolean
''' <summary>
''' ctor
''' </summary>
''' <param name="filepath">path of a target csv file</param>
''' <remarks>fixed encoding. shift-jis</remarks>
Public Sub New(ByVal filepath As String)
Me.filepath = filepath
headerRow = True
encoding = encoding.GetEncoding(932)
End Sub
''' <summary>
''' making a list of string arrays
''' each rows in the csv -> string array
''' </summary>
''' <returns>list of string arrays</returns>
''' <remarks>if headerRow=true, delete the first row(remove the first element of list)</remarks>
Function toList() As List(Of String())
Dim reader As New TextFieldParser(filepath, encoding)
reader.TextFieldType = FieldType.Delimited
reader.SetDelimiters(",")
Dim datalist As New List(Of String())
While Not reader.EndOfData
Dim currentrow As String()
currentrow = reader.ReadFields
datalist.Add(currentrow)
End While
If headerRow Then
datalist.RemoveAt(0)
Return datalist
Else
Return datalist
End If
End Function
''' <summary>
''' you can specify needed columns
''' </summary>
''' <param name="filter">integer array. index starts from 0.</param>
''' <returns>list of string arrays</returns>
''' <remarks>overload</remarks>
Function toList(ByVal filter As Integer()) As List(Of String())
Dim datalist As List(Of String()) = toList()
Dim filteredList As New List(Of String())
For Each Item As String() In datalist
Dim newlist As New List(Of String)
For Each index As Integer In filter
newlist.Add(Item(index))
Next
filteredList.Add(newlist.ToArray)
Next
Return filteredList
End Function
End Class