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
| Public Function Read() As DataTable
'
RaiseEvent StartRead(Me, New System.EventArgs)
'
Dim Table1 As New DataTable
Dim fs As FileStream = Nothing
Dim sr As StreamReader = Nothing
Dim Initialized_Columns As Boolean
'
Try
'Déclarations
fs = New FileStream(NomFichier, FileMode.Open)
sr = New StreamReader(fs, System.Text.Encoding.UTF8)
Dim Ligne As String = ""
'
'Création des colonnes
If EnteteColonnes Then
Ligne = sr.ReadLine
Dim ColumnsName() As String = Split(Ligne, Separateur)
For Each Column As String In ColumnsName
Table1.Columns.Add(Column, GetType(String))
Next
End If
'
While Not sr.EndOfStream
'
'Lecture de la ligne et séparation des champs
Ligne = sr.ReadLine
MsgBox(Ligne)
Dim Cells() As String = Split(Ligne, Separateur)
'
'Crée les entêtes de colonnes par défaut si la première ligne ne comporte pas le nom des colones
If Not EnteteColonnes And _
Not Initialized_Columns Then
For i = 0 To Cells.Length - 1
Table1.Columns.Add("Colonne " & i, GetType(String))
Next
Initialized_Columns = True
End If
'
'Ajout des champs dans la table
Dim Row As DataRow = Table1.NewRow
For i = LBound(Cells) To UBound(Cells)
If InStr(Cells(i), vbLf) <> 0 Then
Replace(Cells(i), vbLf, " ")
End If
Row.Item(i) = Cells(i)
Next
Table1.Rows.Add(Row)
'
End While
'
RaiseEvent EndRead(Me, New CSVEventArgs)
'
Return Table1
'
Catch ex As Exception
'
Dim EA As New CSVEventArgs
EA.Error = ex
RaiseEvent EndRead(Me, EA)
Return Table1
'
Finally
'
'Libération des ressources
If sr IsNot Nothing Then sr.Close()
If sr IsNot Nothing Then sr.Dispose()
If fs IsNot Nothing Then fs.Dispose()
'
End Try
'
End Function |
Partager