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 216 217
| Imports System.IO
'
Public Class CSV
'
#Region "Champs privés"
'
Public _FirstLineIsColumnHeader As Boolean
'
Private _Separator As String = ";"
'
Private _FileName As String
'
#End Region
'
#Region "événements publiques"
'
Public Event StartRead(ByVal sender As Object, ByVal e As System.EventArgs)
'
Public Event EndRead(ByVal sender As Object, ByVal e As CSVEventArgs)
'
Public Event StartWrite(ByVal sender As Object, ByVal e As System.EventArgs)
'
Public Event EndWrite(ByVal sender As Object, ByVal e As CSVEventArgs)
'
#End Region
'
#Region "Propriétés publiques"
'
Public Property FirstLineIsColumnHeader() As Boolean
Get
Return _FirstLineIsColumnHeader
End Get
Set(ByVal value As Boolean)
_FirstLineIsColumnHeader = value
End Set
End Property
'
Public Property FileName() As String
Get
Return _FileName
End Get
Set(ByVal value As String)
_FileName = value
End Set
End Property
'
Public Property Separator() As String
Get
Return _Separator
End Get
Set(ByVal value As String)
_Separator = value
End Set
End Property
'
#End Region
'
#Region "Fonctions publiques"
'
Public Function Read() As DataTable
'
RaiseEvent StartRead(Me, New System.EventArgs)
'
Dim Ret As New DataTable
Dim fs As FileStream = Nothing
Dim sr As StreamReader = Nothing
Dim Initialized_Columns As Boolean
'
Try
'Déclarations
fs = New FileStream(_FileName, FileMode.Open)
sr = New StreamReader(fs, System.Text.Encoding.UTF7)
Dim Line As String = ""
'
'Création des colonnes
If _FirstLineIsColumnHeader Then
Line = sr.ReadLine
Dim ColumnsName() As String = Split(Line, _Separator)
For Each Column As String In ColumnsName
Ret.Columns.Add(Column, GetType(String))
Next
End If
'
While Not sr.EndOfStream
'
'Lecture de la ligne et séparation des champs
Line = sr.ReadLine
Dim Cells() As String = Split(Line, _Separator)
'
'Crée les entêtes de colonnes par défaut si la première ligne
'ne comporte pas le nom des colones
If Not _FirstLineIsColumnHeader And _
Not Initialized_Columns Then
For i = 0 To Cells.Length - 1
Ret.Columns.Add("Colonne " & i, GetType(String))
Next
Initialized_Columns = True
End If
'
'Ajout des champs dans la table
Dim Row As DataRow = Ret.NewRow
For i = LBound(Cells) To UBound(Cells)
Row.Item(i) = Cells(i)
Next
Ret.Rows.Add(Row)
'
End While
'
RaiseEvent EndRead(Me, New CSVEventArgs)
'
Return Ret
'
Catch ex As Exception
'
Dim EA As New CSVEventArgs
EA.Error = ex
RaiseEvent EndRead(Me, EA)
Return Ret
'
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
'
Public Function Write(ByVal Data As DataTable) As Boolean
'
RaiseEvent StartWrite(Me, New System.EventArgs)
'
Dim fs As FileStream = Nothing
Dim sw As StreamWriter = Nothing
'
Try
'
'Supprime le fichier cible si existant
If File.Exists(_FileName) Then _
IO.File.Delete(_FileName)
'
'Ouvre/crée le fichier
fs = New FileStream(_FileName, FileMode.CreateNew, FileAccess.Write)
sw = New StreamWriter(fs, System.Text.Encoding.UTF7)
'
'Ecrit les entêtes de colonnes selon l'option choisie
If _FirstLineIsColumnHeader Then
Dim FirstLine As String = ""
For Each Col As DataColumn In Data.Columns
FirstLine &= Col.Caption & _Separator
Next
FirstLine = FirstLine.Remove(FirstLine.Length - 1, 1)
sw.WriteLine(FirstLine)
End If
'
'Ecrit les lignes
For Each Row As DataRow In Data.Rows
Dim NewLine As String = ""
For i = 0 To Data.Columns.Count - 1
NewLine &= Row.Item(i) & _Separator
Next
NewLine = NewLine.Remove(NewLine.Length - 1, 1)
sw.WriteLine(NewLine)
Next
'
RaiseEvent EndRead(Me, New CSVEventArgs)
'
Return True
'
Catch ex As Exception
'
Dim EA As New CSVEventArgs
EA.Error = ex
RaiseEvent EndRead(Me, EA)
Return False
'
Finally
'
'Libération des ressources
If sw IsNot Nothing Then sw.Close()
If sw IsNot Nothing Then sw.Dispose()
If fs IsNot Nothing Then fs.Dispose()
'
End Try
''
End Function
'
#End Region
'
End Class
'
Public Class CSVEventArgs
Inherits System.EventArgs
'
#Region "Champs privés"
'
Private _Error As Exception
'
#End Region
'
#Region "Propriétés publiques"
'
Property [Error]() As Exception
Get
Return _Error
End Get
Friend Set(ByVal value As Exception)
_Error = value
End Set
End Property
'
#End Region
'
End Class
' |
Partager