Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Importar datos de Excel a un StringGrid. (https://www.clubdelphi.com/foros/showthread.php?t=74410)

ingabraham 17-06-2011 15:25:41

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;

roman 17-06-2011 15:40:37

Acceder a datos de Excel mediante OLE, que es lo que haces tú, es extremadamente lento. Es mucho más eficiente utilizar ADO. En este hilo se explica como pasar datos de excel a paradox pero la parte de paradox la puedes sustituir por un stringgrid. Lo importante es que te explica cómo configurar ADO para conectarte a una hoja de Excel.

// Saludos

ingabraham 17-06-2011 16:42:46

el ADO no me kiere importar archivos de excel .xlsx

Código Delphi [-]
error .  la tabla externa no tiene el formato esperado

roman 17-06-2011 16:47:51

Cita:

Empezado por ingabraham (Mensaje 404072)
el ADO no me kiere importar archivos de excel .xlsx

¡Ah! Intenta esto.

// Saludos

ingabraham 19-06-2011 00:49:00

ok. ya hice la exportacion excelente por ADO. es rapida
mas de 2.000. registros al strincell..


1. ahora necesito una barra de progreso que vaya concorde a la importacion de estos datos a mi tabla( que sea real y que se vaya viendo al momento de ir insertando datos)

Código Delphi [-]

insert into clientes value(:a,:b,....)
parametro (a)  = stringcel(0)
parametro (b)  = stringcel(1)
execute

esta importacion, es bien lenta, no se si armando un procedimiento almacenado seria mas rapido. quien me da alguna idea.


La franja horaria es GMT +2. Ahora son las 12:37:15.

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