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
| Option Explicit
Dim T As Long, U As Long, NumFich As Integer
Dim MonFichier As String, Chaine As String, TblCol() As String, SeparateuR As String
Dim DebuTmp As Single, FinTmp As Single
Private Sub Form_Load()
'positionne les élements
Command1.Move 120, 60, 1035, 315: Command1.Caption = "Ouvrir"
Command2.Move 1200, 60, 1035, 315: Command2.Caption = "Enregistrer"
Command3.Move 120, 420, 2115, 315: Command3.Caption = "Vider le FlexGrid"
MSFlexGrid1.Move 0, 1140, 5175, 3075
MSFlexGrid1.Rows = 10
MSFlexGrid1.FixedRows = 1
MSFlexGrid1.Cols = 5
MSFlexGrid1.FixedCols = 0
Me.Height = 4800: Me.Width = 5415
Option1(0).Move 2400, 60, 1455, 255: Option1(0).Caption = ".TextMatrix(L,C)": Option1(0).ToolTipText = "Ouverture ou enregistrement"
Option1(1).Move 3960, 60, 795, 255: Option1(1).Caption = ".Clip": Option1(1).ToolTipText = "Ouverture ou enregistrement"
Label1.Move 2460, 480
Label1.Caption = "": Label1.AutoSize = True
Label2.Move 60, 900
Label2.Caption = "": Label2.AutoSize = True
'Histoire de remplir le grid
MSFlexGrid1.FormatString = "colonne 1|colonne 2|colonne 3 |colonne 4|colonne 5" 'titrage des colonnes
For T = 1 To MSFlexGrid1.Rows - 1
For U = 0 To MSFlexGrid1.Cols - 1
MSFlexGrid1.TextMatrix(T, U) = Chr(64 + T + U) 'remplis chaque celulle
Next U
Next T
SeparateuR = vbTab
End Sub
Private Sub Command1_Click()
'Ouvrir
If Option1(0).Value = True Then
CommonDialog1.DialogTitle = "Ouvrir un fichier méthode .TextMatrix"
Else
CommonDialog1.DialogTitle = "Ouvrir un fichier méthode .Clip"
End If
CommonDialog1.CancelError = True
On Error Resume Next
CommonDialog1.ShowOpen
If Err = cdlCancel Then Exit Sub
Err.Clear
MonFichier = CommonDialog1.FileName
NumFich = FreeFile
Open MonFichier For Input As #NumFich
Label1.Caption = "en cours"
DebuTmp = Timer
If Option1(0).Value = True Then
'------------------ par méthode .TextMatrix(numéro Ligne,numéro Colonne) -----------------------------------
MSFlexGrid1.Clear 'vider toutes les celulles du grid
MSFlexGrid1.Rows = 1: MSFlexGrid1.Cols = 1: T = 1
Do While Not EOF(NumFich) ' Effectue la boucle jusqu'à la fin du fichier.
Line Input #NumFich, Chaine ' recuperation de la ligne
TblCol = Split(Chaine, SeparateuR) 'eclate la ligne en colonne
If T = 1 Then MSFlexGrid1.Cols = UBound(TblCol) + 1 'dimensionne le nombre de colonne du grid (une seule fois)
MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1 'ajoute une ligne au grid
For U = 0 To MSFlexGrid1.Cols - 1 'pour rempli chaque colonne
MSFlexGrid1.TextMatrix(T, U) = TblCol(U)
Next U
T = T + 1 'préparation pour la suite
Loop
Close NumFich
Me.Refresh 'pour les gros fichiers
Else
'------------------ par méthode .Clip -----------------------------------
Chaine = Input(FileLen(MonFichier), NumFich) 'récupération de lentièreté du fichier
Close #NumFich
If Chaine = "" Then
MsgBox "le fichier est vide", vbCritical
Exit Sub
End If
If Right(Chaine, 2) = vbNewLine Then Chaine = Left(Chaine, Len(Chaine) - 2)
'pour Debuguer
'For T = 1 To 16
' Debug.Print Mid(Chaine, T, 1) & " ascii = " & Asc(Mid(Chaine, T, 1))
'Next T
'Chaine = Replace(Chaine, Chr(13), vbCr)
With MSFlexGrid1
.Clear
.Rows = (UBound(Split(Chaine, vbNewLine))) + 2 'vbNewLine = separateur de ligne
.Cols = (UBound(Split(Split(Chaine, vbNewLine)(0), vbTab))) + 1 'vbTab = separateur de colonne
.RowSel = .Rows - 1
.ColSel = .Cols - 1
.Clip = Chaine
.Row = 1 'pour dé-sélectionner les cellules
End With
End If
FinTmp = Timer
Label1.Caption = FinTmp - DebuTmp
Label2.Caption = MonFichier
End Sub
Private Sub Command2_Click()
'Enregistrer
If Option1(0).Value = True Then
CommonDialog1.DialogTitle = "Enregistrer le FlexGrid méthode .TextMatrix"
Else
CommonDialog1.DialogTitle = "Enregistrer le FlexGrid méthode .Clip"
End If
CommonDialog1.CancelError = True
On Error Resume Next
CommonDialog1.ShowSave
If Err = cdlCancel Then Exit Sub
Err.Clear
MonFichier = CommonDialog1.FileName
NumFich = FreeFile
Open MonFichier For Output As #NumFich
Label1.Caption = "en cours"
DebuTmp = Timer
If Option1(0).Value = True Then
'------------------ par méthode .TextMatrix(numéro Ligne,numéro Colonne) -----------------------------------
For T = 1 To MSFlexGrid1.Rows - 1
Chaine = ""
For U = 0 To MSFlexGrid1.Cols - 1
Chaine = Chaine & MSFlexGrid1.TextMatrix(T, U)
'pour ne pas ajouter un separateur à la derniére colonne
If U <> MSFlexGrid1.Cols - 1 Then Chaine = Chaine & SeparateuR
Next U
Print #NumFich, Chaine 'écrit la ligne
Next T
Else
'------------------ par méthode .Clip -----------------------------------
With MSFlexGrid1
.Row = 1
.Col = 0
.RowSel = .Rows - 1
.ColSel = .Cols - 1
Chaine = .Clip
Print #NumFich, Chaine 'écrit la ligne
.Row = 1 'pour dé-sélectionner les cellules
End With
End If
Close NumFich
FinTmp = Timer
Label1.Caption = FinTmp - DebuTmp
Label2.Caption = MonFichier
Me.SetFocus: Me.Refresh 'pour les gros fichiers
End Sub
Private Sub Command3_Click()
'Vider le FlexGrid
MSFlexGrid1.Clear
End Sub |
Partager