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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
| {------------------------------------------------------------------------------}
{ }
{------------------------------------------------------------------------------}
{------------------------------------------------------------------------------}
{ Cette unité s'occupe de la gestion de la fiche principale. }
{------------------------------------------------------------------------------}
unit Unit1;
interface
uses Windows, SysUtils, Classes, Controls, Forms, Dialogs, Grids, StdCtrls, Math,
ComCtrls, ToolWin, ImgList, ExtCtrls, XPStyleActnCtrls, ActnList, ActnMan,
StdActns, abfComponents, abfStatusBars;
type
{Type permettant de récupérer les données du fichier csv}
TCsvExtract =
Record
TabResult : Array of Array of ShortString; {Tableau contenant les données du fichier}
end;
TForm1 = class(TForm)
StringGrid1: TStringGrid;
GroupBoxSepar: TGroupBox;
CheckBoxTab: TCheckBox;
CheckBoxSpace: TCheckBox;
CheckBoxPtComma: TCheckBox;
CheckBoxOther: TCheckBox;
CheckBoxComma: TCheckBox;
EditOther: TEdit;
ToolBar1: TToolBar;
ButOpen: TToolButton;
ToolButton2: TToolButton;
ButAutoSizeCol: TToolButton;
ButEraseRow: TToolButton;
ToolButton5: TToolButton;
ButAbout: TToolButton;
ImageList1: TImageList;
abfStatusBar1: TabfStatusBar;
SauverSous1: TToolButton;
SaveDialog1: TSaveDialog;
procedure ButOpenClick(Sender: TObject);
procedure ButAboutClick(Sender: TObject);
procedure ButAutoSizeColClick(Sender: TObject);
procedure SauverSous1Click(Sender: TObject);
private
{ Déclarations privées }
procedure ExtractCsvData(BoolTab,BoolPtComma,BoolComma,BoolSpace,BoolOther,EraseRow : Boolean;
SeparOther : ShortString; FileName : AnsiString; var CsvExtract : TCsvExtract);
procedure ResizeColumns(StrGrd : TStringGrid);
public
{ Déclarations publiques }
end;
var
Form1 : TForm1;
NomFichier : String;
implementation
uses Boite_Propos;
{$R *.dfm}
{------------------------------------------------------------------------------}
{ Exportation des données du fichier dans un tableau. }
{------------------------------------------------------------------------------}
procedure TForm1.ExtractCsvData(BoolTab,BoolPtComma,BoolComma,BoolSpace,BoolOther,EraseRow : Boolean;
SeparOther : ShortString; FileName : AnsiString; var CsvExtract : TCsvExtract);
const
SeparTab = Chr(9);
SeparPtComma = ';';
SeparComma = ',';
SeparSpace = ' ';
{Récupèration de la position des séparateurs}
function RecupPosSepar(BoolTab,BoolPtComma,BoolComma,BoolSpace,BoolOther : Boolean;
SeparOther : ShortString; Text : String) : Integer;
var
I : Word;
P : Array[1..5] of Integer;
begin
{On initialise les positions des séparateurs a une valeur}
For I:=1 to 5 do P[I] := 999999999;
{Si le séparateur est démandé et que sa position est > 0 alors on conserve sa position}
If (BoolTab) and (Pos(SeparTab,Text) > 0) then P[1] := Pos(SeparTab,Text);
If (BoolPtComma) and (Pos(SeparPtComma,Text) > 0) then P[2] := Pos(SeparPtComma,Text);
If (BoolComma) and (Pos(SeparComma,Text) > 0) then P[3] := Pos(SeparComma,Text);
If (BoolSpace) and (Pos(SeparSpace,Text) > 0) then P[4] := Pos(SeparSpace,Text);
If (BoolOther) and (Pos(SeparOther,Text) > 0) then P[5] := Pos(SeparOther,Text);
{On prend la plus petite valeur pour avoir le séparateur le premier séparateur}
Result := MinIntValue(P);
{Si le résultat égale la valeur de départ alors on dit que le resultat = 0
sinon s il manque le séparateur en fin de ligne on perdra la derniere lettre}
If Result = 999999999 then Result := 0;
end;
{On scanne tous les items et ceux dans lequel on trouve # ou bien rien et bien on les supprime}
procedure CleanList(var ListTemp : TStringList);
var
I : Integer;
begin
I := 0;
While I < ListTemp.Count do
begin
{Dabord on verifie que le champ soit remplit sinon erreur
erreur quand on fait juste un test sur le premier chr}
If (Length(ListTemp.Strings[I]) = 0) then ListTemp.Delete(I)
else
begin
If (ListTemp.Strings[I][1] = '#') then ListTemp.Delete(I)
else Inc(I);
end;
end;
end;
{Récupération des différents champs}
function RecupItems(Ligne : String) : TStringList;
var
P0 : Integer;
begin
{On crée la liste de sortie}
Result := TStringList.Create;
{On recherche les ; et on insere les différents item dans la tableau}
P0 := RecupPosSepar(BoolTab,BoolPtComma,BoolComma,BoolSpace,BoolOther,SeparOther,Ligne);
While P0 > 0 do
begin
{On insere le champ}
Result.Add(Copy(Ligne,1,P0-1));
{On supprime ce champ du flux de données}
Delete(Ligne,1,P0);
{On récupère le champ suivant}
P0 := RecupPosSepar(BoolTab,BoolPtComma,BoolComma,BoolSpace,BoolOther,SeparOther,Ligne);
end;
{S'il n'y a pas de séparateur à la fin de la ligne}
If Length(Ligne) > 0 then Result.Add(Ligne);
end;
var
List,Items : TStringList;
I,J : Integer;
begin
{On crée dynamiquement la liste qui va contenir les lignes du fichier}
List := TStringList.Create;
Try
If FileExists(FileName) then
begin
{On charge le fichier}
List.LoadFromFile(FileName);
{On détruit les lignes vides ou commencant par #}
If EraseRow then CleanList(List);
{On initialise le tableau}
SetLength(CsvExtract.TabResult,0,List.Count);
{On fait toutes les lignes du fichier}
For I:=0 to List.Count-1 do
begin
{On recupere les différents champ de la ligne}
Items := RecupItems(List.Strings[I]);
Try
{On définit la taille du tableau}
If High(CsvExtract.TabResult) < Items.Count then
SetLength(CsvExtract.TabResult,Items.Count,List.Count);
{On ajoute les lignes de la liste au tableau}
For J:=0 to Items.Count-1 do
CsvExtract.TabResult[J,I] := Items.Strings[J];
Finally
Items.Free;
end;
end;
end
else
ShowMessage('Le chemin suivant : "' + FileName + '" n''est pas valide.');
finally
{On détruit le tstringlist pour libérer la mémoire}
List.Free;
end;
end;
{------------------------------------------------------------------------------}
{ Appui sur le bouton ouvrir. }
{------------------------------------------------------------------------------}
procedure TForm1.ButOpenClick(Sender: TObject);
var
I,J,X,Y : Integer;
OpenDialog1 : TOpenDialog;
CsvExtract1 : TCsvExtract;
begin
{On crée dynamiquement la boite d'ouverture de fichier}
OpenDialog1 := TOpenDialog.Create(nil);
Try
{On sélectionne les types de fichiers que la boite peut ouvrir}
OpenDialog1.Filter := 'Text Files (*.txt)|*.txt|Csv Files (*.csv)|*.csv|All Files (*.*)|*.*';
If OpenDialog1.Execute then
begin
{On lance analyse du fichier}
ExtractCsvData(CheckBoxTab.Checked,CheckBoxPtComma.Checked,CheckBoxComma.Checked,
CheckBoxSpace.Checked,CheckBoxOther.Checked,ButEraseRow.Down,EditOther.Text,
OpenDialog1.FileName,CsvExtract1);
{On détemrine le nombre de ligne du tableau}
X := High(CsvExtract1.TabResult);
Y := High(CsvExtract1.TabResult[1]);
StringGrid1.RowCount := Y + 2;
StringGrid1.ColCount := X + 1;
{On affiche les données dans le stringrid}
For I:=0 to X do
begin
StringGrid1.Cells[I,0] := 'Item' + IntToStr(I);
For J:=0 to Y do
StringGrid1.Cells[I,J+1] := CsvExtract1.TabResult[I,J];
end;
end;
{On appel la procedure de redimensionnent des colonnes}
ButAutoSizeColClick(nil);
finally
{On détruit la boite d'ouverture de fichier pour libérer la mémoire}
OpenDialog1.Free;
end;
end;
{------------------------------------------------------------------------------}
{ Redimensionnement automatique des colonnes d'un tableau. }
{------------------------------------------------------------------------------}
procedure TForm1.ResizeColumns(StrGrd : TStringGrid);
var
I,J,MaxTextWidth : Integer;
begin
{On fait toutes les colonnes du StringGrid}
For J:=0 to StrGrd.ColCount-1 do
begin
{Initialisation}
MaxTextWidth := 0;
{On recupere la longueur la plus grande contenu dans la colonne}
For I:=0 to StrGrd.RowCount - 1 do
MaxTextWidth := Max(MaxTextWidth,StrGrd.Canvas.TextWidth(StrGrd.Cells[J,I]));
{On applique la nouvelle taille a condition que la colonne n'était pas vide}
If MaxTextWidth > 0 then StrGrd.ColWidths[J] := MaxTextWidth + 5;
end;
end;
{------------------------------------------------------------------------------}
{ Click sur le bouton a propos. }
{------------------------------------------------------------------------------}
procedure TForm1.ButAboutClick(Sender: TObject);
begin
Application.CreateForm(TAboutBox,AboutBox);
AboutBox.ShowModal;
end;
{------------------------------------------------------------------------------}
{ Click sur le ToolBouton d'ajustement automatique des colonnes. }
{------------------------------------------------------------------------------}
procedure TForm1.ButAutoSizeColClick(Sender: TObject);
begin
{Si l'utilisateur a cocher le redimensionnement automatique on lance la procedure}
If ButAutoSizeCol.Down then ResizeColumns(StringGrid1);
end;
{---------------------------------------------------------}
{ Enregistrer le contenu d'une StringGrid, dans un fichier}
{ fichier texte dans lequel les champs seront séparés par }
{ un ou plusieurs caracters spécifiques. Ce type de }
{ fichier est compatible avec EXCEL. }
{---------------------------------------------------------}
Function SauveGrilleDansTable(Nom : String;Grille : TStringGrid;
Separateur : Char) : Boolean;
Var
F : TextFile;
Chn,Mot : String;
I,J,X,Y : Integer;
Begin
Result:=True;
// Renomer le fichier avec l'extension .bak si il existe
If FileExists(Nom) Then
begin
If FileExists(ChangeFileExt(Nom,'.bak')) Then
DeleteFile(ChangeFileExt(Nom,'.bak'));
RenameFile(Nom,ChangeFileExt(Nom,'.bak'));
end;
// Créer le fichier
AssignFile(F,Nom);
Rewrite(F);
I:=Grille.ColCount;
J:=Grille.RowCount;
For Y:=1 To J Do
begin
Chn:='';
For X:=1 To I Do // Construire la chaine.
begin
// lire toutes les cellules de la grille et Construire la chaine.
Mot:=Grille.Cells[X,Y];
If Length(Mot) = 0 Then
begin
If Separateur <> #32 Then Mot:=#32
Else Mot:='_';
end;
Chn:=Chn + Mot + Separateur;
end;
// Supprimer le dernier séparateur inutile.
Delete(Chn,Length(Chn),1);
// Ecrire la ligne dans le fichier
WriteLn(F,Chn);
If IoResult <> 0 Then
begin
Result:=False;
Exit;
end;
end;
CloseFile(F);
End;
{------------------------------------------------------------------------------}
{ Click sur le bouton Sauver. }
{------------------------------------------------------------------------------}
procedure TForm1.SauverSous1Click(Sender: TObject);
begin
If SaveDialog1.Execute Then
begin
NomFichier := SaveDialog1.FileName;
SauveGrilleDansTable(NomFichier, StringGrid1, ';');
end;
end;
end. |
Partager