Ver Mensaje Individual
  #1  
Antiguo 17-06-2011
Avatar de ingabraham
ingabraham ingabraham is offline
Miembro
 
Registrado: ago 2007
Posts: 614
Reputación: 17
ingabraham Va por buen camino
Importar datos de Excel a un StringGrid.

Dos problemas

1. hacer esta importacion mas rapida. ya q importo mas de 2.000 registros con 60 campo cda uno. ya que se vuelve muy lenta.

2. mostrar una barra de progreso que vaya acorde al numero de registro en tiempo real. bueno para que el usuario tenga idea lo que se va a demorar

mi codigo es el sgte.

Código Delphi [-]

 OpenDialog1.Execute then
    Archivo.Text:=OpenDialog1.FileName;

If  Archivo.Text<>'' then
Begin

   Screen.Cursor := crHourGlass;

   LimpiarStringGrid();

   SpeedButton5.Enabled :=false;
   SpeedButton2.Enabled :=false;

   try
   Excel := CreateOleObject('Excel.Application');
   except
     Excel.Quit;
     showmessage('No se pudo crear el Objecto Excel.');
    raise;
   end;


Try

 salida:=false;
 Excel.Workbooks.Open(Archivo.text);
   j:=2;   f:=1;
   str:= Excel.Cells[j,1].Value;//inicialmente cedula
   if (str='')then
    salida:=true;

   While not salida do
   begin


        StringGrid1.Cells[0,f]:= Excel.Cells[j,1].Value;   //DATOIMPORTADO
        StringGrid1.Cells[1,f]:= Excel.Cells[j,2].Value;   //DATOIMPORTADO
        StringGrid1.Cells[2,f]:= Excel.Cells[j,3].Value;   //DATOIMPORTADO
        StringGrid1.Cells[3,f]:= Excel.Cells[j,4].Value;   //DATOIMPORTADO
        StringGrid1.Cells[4,f]:= Excel.Cells[j,5].Value;   //DATOIMPORTADO
        StringGrid1.Cells[5,f]:= Excel.Cells[j,6].Value;   //DATOIMPORTADO
        StringGrid1.Cells[6,f]:= Excel.Cells[j,7].Value;   //DATOIMPORTADO
        StringGrid1.Cells[7,f]:= Excel.Cells[j,8].Value;   //DATOIMPORTADO
        StringGrid1.Cells[8,f]:= Excel.Cells[j,9].Value;   //DATOIMPORTADO
        StringGrid1.Cells[9,f]:= Excel.Cells[j,10].Value;   //DATOIMPORTADO


        StringGrid1.Cells[10,f]:= Excel.Cells[j,11].Value;   //DATOIMPORTADO
        StringGrid1.Cells[11,f]:= Excel.Cells[j,12].Value;   //DATOIMPORTADO
        StringGrid1.Cells[12,f]:= Excel.Cells[j,13].Value;   //DATOIMPORTADO
        StringGrid1.Cells[13,f]:= Excel.Cells[j,14].Value;   //DATOIMPORTADO
        StringGrid1.Cells[14,f]:= Excel.Cells[j,15].Value;   //DATOIMPORTADO
        StringGrid1.Cells[15,f]:= Excel.Cells[j,16].Value;   //DATOIMPORTADO
        StringGrid1.Cells[16,f]:= Excel.Cells[j,17].Value;   //DATOIMPORTADO
        StringGrid1.Cells[17,f]:= Excel.Cells[j,18].Value;   //DATOIMPORTADO
        StringGrid1.Cells[18,f]:= Excel.Cells[j,19].Value;   //DATOIMPORTADO
        StringGrid1.Cells[19,f]:= Excel.Cells[j,20].Value;   //DATOIMPORTADO

        StringGrid1.Cells[20,f]:= Excel.Cells[j,21].Value;   //P_CEDULA
        StringGrid1.Cells[21,f]:= Excel.Cells[j,22].Value;   //P_CIUDAD_EXP_CED
        StringGrid1.Cells[22,f]:= Excel.Cells[j,23].Value;   //P_NOMBRE
        StringGrid1.Cells[23,f]:= Excel.Cells[j,24].Value;   //P_DIRECCION
        StringGrid1.Cells[24,f]:= Excel.Cells[j,25].Value;   //P_TELEFONO
        StringGrid1.Cells[25,f]:= Excel.Cells[j,26].Value;   //P_NRO_RESOLUCION
        StringGrid1.Cells[26,f]:= Excel.Cells[j,27].Value;   //P_ANNO_RESOLUCION
        StringGrid1.Cells[27,f]:= Excel.Cells[j,28].Value;   //P_FECHA_EXPEDICION
        StringGrid1.Cells[28,f]:= Excel.Cells[j,29].Value;   //P_NRO_RESOLUCION
        StringGrid1.Cells[29,f]:= Excel.Cells[j,30].Value;   //P_ANNO_RESOLUCION

        StringGrid1.Cells[30,f]:= Excel.Cells[j,31].Value;   //P_CEDULA
        StringGrid1.Cells[31,f]:= Excel.Cells[j,32].Value;   //P_CIUDAD_EXP_CED
        StringGrid1.Cells[32,f]:= Excel.Cells[j,33].Value;   //P_NOMBRE
        StringGrid1.Cells[33,f]:= Excel.Cells[j,34].Value;   //P_DIRECCION
        StringGrid1.Cells[34,f]:= Excel.Cells[j,35].Value;   //P_TELEFONO
        StringGrid1.Cells[35,f]:= Excel.Cells[j,36].Value;   //P_NRO_RESOLUCION
        StringGrid1.Cells[36,f]:= Excel.Cells[j,37].Value;   //P_ANNO_RESOLUCION
        StringGrid1.Cells[37,f]:= Excel.Cells[j,38].Value;   //P_FECHA_EXPEDICION
        StringGrid1.Cells[38,f]:= Excel.Cells[j,39].Value;   //P_NRO_RESOLUCION
        StringGrid1.Cells[39,f]:= Excel.Cells[j,40].Value;   //P_ANNO_RESOLUCION

        StringGrid1.Cells[40,f]:= Excel.Cells[j,41].Value;   //P_CEDULA
        StringGrid1.Cells[41,f]:= Excel.Cells[j,42].Value;   //P_CIUDAD_EXP_CED
        StringGrid1.Cells[42,f]:= Excel.Cells[j,43].Value;   //P_NOMBRE
        StringGrid1.Cells[43,f]:= Excel.Cells[j,44].Value;   //P_DIRECCION
        StringGrid1.Cells[44,f]:= Excel.Cells[j,45].Value;   //P_TELEFONO
        StringGrid1.Cells[45,f]:= Excel.Cells[j,46].Value;   //P_NRO_RESOLUCION
        StringGrid1.Cells[46,f]:= Excel.Cells[j,47].Value;   //P_ANNO_RESOLUCION
        StringGrid1.Cells[47,f]:= Excel.Cells[j,48].Value;   //P_FECHA_EXPEDICION
        StringGrid1.Cells[48,f]:= Excel.Cells[j,49].Value;   //P_NRO_RESOLUCION
        StringGrid1.Cells[49,f]:= Excel.Cells[j,50].Value;   //P_ANNO_RESOLUCION

        StringGrid1.Cells[50,f]:= Excel.Cells[j,51].Value;   //P_CEDULA
        StringGrid1.Cells[51,f]:= Excel.Cells[j,52].Value;   //P_CIUDAD_EXP_CED
        StringGrid1.Cells[52,f]:= Excel.Cells[j,53].Value;   //P_NOMBRE
        StringGrid1.Cells[53,f]:= Excel.Cells[j,54].Value;   //P_DIRECCION
        StringGrid1.Cells[54,f]:= Excel.Cells[j,55].Value;   //P_TELEFONO
        StringGrid1.Cells[55,f]:= Excel.Cells[j,56].Value;   //P_NRO_RESOLUCION
        StringGrid1.Cells[56,f]:= Excel.Cells[j,57].Value;   //P_ANNO_RESOLUCION
        StringGrid1.Cells[57,f]:= Excel.Cells[j,58].Value;   //P_FECHA_EXPEDICION
        StringGrid1.Cells[58,f]:= Excel.Cells[j,59].Value;   //P_NRO_RESOLUCION

       StringGrid1.RowCount:=StringGrid1.RowCount+1;//agrega otra fila
       inc(j);
       inc(f);

       str:= Excel.Cells[j,1].Value;
       if (str='')then
         salida:=true;
    end;

     Excel.Quit;


   except
    begin
     Excel.Quit;
     ShowMessage(' !!!!! ATENCION !!!!! ' + chr(13) +' El fichero no pudo ser importado' + chr(13) +' Revise bien su estructura');
     end;
    End;

End;
//....
   SpeedButton5.Enabled :=true;
   SpeedButton2.Enabled :=true;

   Screen.Cursor := crDefault;
__________________
Enseñar es la virtud de un sabio.

Última edición por ingabraham fecha: 17-06-2011 a las 15:30:58.
Responder Con Cita