PDA

Ver la Versión Completa : lazarus - importar archivo .txt a grilla


alebritez
13-05-2016, 04:52:42
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 (http://www.clubdelphi.com/foros/guiaestilo.php).

El ejemplo,

...
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:

...
begin
FileToGridWithFixed( 'ARCHIVO.TXT', StringGrid1 );
end;


Saludos :)

Ñuño Martínez
13-05-2016, 11:39:34
¿Grilla (http://www.wordreference.com/es/en/translation.asp?spen=grilla)? :confused: