Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Lazarus, FreePascal, Kylix, etc. (https://www.clubdelphi.com/foros/forumdisplay.php?f=14)
-   -   lazarus - importar archivo .txt a grilla (https://www.clubdelphi.com/foros/showthread.php?t=90294)

alebritez 13-05-2016 04:52:42

lazarus - importar archivo .txt a grilla
 
Buenas, estoy comenzando a utilizar lazarus, y necesito ayuda sobre un tema, espero que puedan ayudarme.
Estoy teniendo un inconveniente al momento de realizar un programa, donde tengo un Archivo .txt con registros donde los datos están separados por ";" y necesito volcarlos en una Grilla, separando cada dato.
Tendrían algún ejemplo de código, ya que la verdad soy nuevo en esto.
Aguardo sus respuestas..
Gracias de antemano !!!

ecfisa 13-05-2016 07:16:37

Hola alebritez, bienvenido a los foros de Club Delphi :)

Como es costumbre con los iniciados te invitamos a leer nuestra Guía de estilo.

El ejemplo,
Código Delphi [-]
...
function CharCount( const ch: Char; const st: string ): Integer;
var
  i: Integer;
begin
  Result := 0;
  for i := 1 to Length( st ) do
    if st[i] = ch then Inc( Result );
end;

procedure FileToGridWithFixed( const aFileName: string; Grid: TStringGrid );
var
  TS: TStrings;
  i : Integer;
begin
  TS := TStringList.Create;
  try
    TS.LoadFromFile( aFileName );
    Grid.RowCount := Grid.FixedRows + TS.Count;
    Grid.ColCount := Grid.FixedCols + CharCount( ';', TS[0] ) + 1;
    for i := 0 to TS.Count-1 do
    begin
      TS[i] := StringOfChar( ';', Grid.FixedCols ) + TS[i];
      Grid.Rows[Grid.FixedRows + i].Delimiter     := ';';
      Grid.Rows[Grid.FixedRows + i].DelimitedText := TS[i];
    end;
  finally
    TS.Free;
  end;
end;

Modo de uso:
Código Delphi [-]
...
begin
   FileToGridWithFixed( 'ARCHIVO.TXT', StringGrid1 );
end;

Saludos :)

Ñuño Martínez 13-05-2016 11:39:34

¿Grilla? :confused:


La franja horaria es GMT +2. Ahora son las 03:34:30.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi