Salut, voici le code que je te suggère. C'est moi qui l'a fait à
100%. Il est facilement adaptable pour n'importe quoi. Puisque je vais
en avoir besoins, je crois qu'il est bon de t'en faire part. Le split
(split()) n'est pas assez efficace. Il ne gère pas des cas spéciaux.
Ce code gère pas mal tout ce qui peux arriver (fichier inexistant, ligne
vide, manque de colonnes, ...). En ce qui conserne le format CSV
comme tel, voilà ce qui permet:
texte du fichier CSV{
"ab;cd";defg
"""ab;cd""";lgsdfgd;gdsgsdf
"allo"""""""
g
}devient
ab:cd defg
"ab;cd" lgsdfgd gdsgsdf
allo"""
NOTE IMPORTANTE: allo"""""" est invalide car il va retourner
allo"". Quand il y a déjà présence du délimiteur de string dans la
valeur, cette valeur doit être entouré de celui-ci. C'est le seul
problème connu.
'Créé par DeadlyPredator le 24 juin 2005
'Je ne suis pas responsable de ce que ce code peut causer
Sub FillListViewWithCSV(ByVal strFile As String, ByRef lvListView As
ListView, Optional ByVal bClearListView As Boolean = True, Optional
ByVal strSeparator As String = ";", Optional ByVal strDelimiter As
String = """", Optional ByVal bAddColsIfNeed As Boolean = False)
On Error Resume Next
Dim hFile As Integer, strLine As String, astrElems() As String, strCar
As String, I As Long, bInStr As Boolean, strLastCar As String,
strBuffer As String, Index As Long, lviLine As ListItem
hFile = FreeFile
If Not ((GetAttr(strFile) And vbNormal) = vbNormal) Then Err.Clear: Exit Sub
If bClearListView Then lvListView.ListItems.Clear
If FileLen(strFile) = 0 Then Exit Sub
Open strFile For Input As hFile
Do While Not EOF(hFile)
Line Input #hFile, strLine
For I = 1 To Len(strLine)
strCar = Mid$(strLine, I, 1)
If strCar = strDelimiter Then
bInStr = Not bInStr
If
(strLastCar = strDelimiter) And bInStr Then strBuffer = strBuffer &
strDelimiter
ElseIf strCar = strSeparator And Not bInStr Then
ReDim Preserve astrElems(Index)
astrElems(Index) = strBuffer
Index = Index + 1
strBuffer = vbNullString
Else
strBuffer = strBuffer & strCar
End If
strLastCar = strCar
Next
If (strBuffer <> vbNullString) Or strCar = strSeparator Then
ReDim Preserve astrElems(Index)
astrElems(Index) = strBuffer
Index = Index + 1
strBuffer = vbNullString
End If
If strLine <> vbNullString Then
Set lviLine = lvListView.ListItems.Add(, , astrElems(0))
If Index > 0 Then
For I = 1 To Index
If ((I + 1) > lvListView.ColumnHeaders.Count) Then
If bAddColsIfNeed Then
lvListView.ColumnHeaders.Add
lviLine.SubItems(I) = astrElems(I)
End If
Else
lviLine.SubItems(I) = astrElems(I)
End If
lviLine.SubItems(I) = astrElems(I)
Next
End If
Else
lvListView.ListItems.Add
End If
'Manipuler la ligne du listview en utilisant lviLine
'...
Index = 0
Set lviLine = Nothing
Erase astrElems
Err.Clear
Loop
Close hFile
End Sub
'exemple d'utilisation
Private Sub Form_Load()
FillListViewWithCSV "c:\Classeur1.csv", Lv
End Sub
'simple comme 100*100=10000
VIVE LE QUÉBEC!
Essayez ça
Dim l As Long: Do Until l = -1: l = l + 1: Loop |