RE:Lectura de archivo .txt
Publicado por
David Escobar (1 intervención) el 12/11/2008 19:27:31
Yo realice el siguiente procedimiento y no me causa problemas...
a ver si te sirve:
procedure TForm1.Button1Click(Sender: TObject);
var
F1: TextFile;
Lines:String;
i,j:integer;
Centro,periodo,fech_pago,F_P,recibo,nombre,rfc,contrato,no_emp,funcion,sub_funci,sub_sub_fun,tp,mtct_pxh,concepto,desc_conp,importe:string;
begin
if OpenDialog1.Execute then // OpenDialog1 Componente no Visual
begin
AssignFile(F1, OpenDialog1.Filename); //lee el archivo
Reset(F1);
// ListBox1.Items.Clear; // por si quieren vacias las lineas del archivo a un componente ListBox1
j:=0;
while not Eof(F1) do
begin
ReadLn(f1,Lines); //lee un linea del archivo
// divide la linea en variables para despues insertalos en una tabla en sus campos respectivos
// Cada linea debera de estan estandariza en el número de caractes y posiciones de caracteres
//ejemplo de dos lineas
//1399201610200800015631MONTESINOS HERNANDEZ JOAN MOHJ810116JC700301110 99900099TC134ARRIAGA PREVISION 000000000200
//1388201610200800437406PALAZUELOS BERISTAIN MA. DEL SOCORRO PABM700322I9300011027 50100066TC001SUELDO O SALARIO 000000070078
//Cada cadena su longitud es de 139 caracteres y
//sus diviciones son similares
// divisicion de la linea en Variables
for i:=1 To Length(Lines) Do
Begin
If i=4 then //posicion de divición
Begin
Centro:=Copy(Lines,1,i); // 1 = inicio de cadena, i = número de caracteres a dividir
j:=i; // j = la posicion del ultimo caracter de la divicion de cadena
End
Else
If i=6 Then //posicion de divición
Begin
periodo:=Copy(Lines,j+1,i-j); // j+1 = inicio de cadena, i-j = número de caracteres a dividir
j:=i; // j = la posicion del ultimo caracter de la divicion de cadena
End Else
If i=14 Then
Begin
F_P:=Copy(Lines,j+1,i-j);
fech_pago:=Copy(F_P,1,2)+'/'+Copy(F_P,3,2)+'/'+Copy(F_P,5,4);
j:=i;
End Else
If i=22 Then
Begin
recibo:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=72 Then
Begin
nombre:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=85 Then
Begin
rfc:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=89 Then
Begin
contrato:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=94 Then
Begin
no_emp:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=95 Then
Begin
funcion:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=97 Then
Begin
sub_funci:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=100 Then
Begin
sub_sub_fun:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=102 Then
Begin
tp:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=104 Then
Begin
mtct_pxh:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=107 Then
Begin
concepto:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=127 Then
Begin
desc_conp:=Copy(Lines,j+1,i-j);
j:=i;
End Else
If i=139 Then
Begin
importe:=Copy(Lines,j+1,i-j);
j:=i;
End
End; //fin del FOR
// ListBox1.Items.Add(Lines); //Si quieren colocar las lineas del archivo en un componente
end; // Fin del WHILE
end; // fin del OpenDialog1
CloseFile(F1);
//Unicamente faltaria la insercion a una tabla.......que ya esta facil no lo creen
end;