Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   ayuda para exportar query a excel (https://www.clubdelphi.com/foros/showthread.php?t=75785)

microbiano 19-09-2011 17:09:27

ayuda para exportar query a excel
 
hola quiero exportar el contenido de una consulta maestro detalle a una hoja de excel y lo hago mediante el siguiente codigo pero no logro imprimir el detalle de cada registro de maestro.


Código Delphi [-]
procedure TForm_requerimiento.btn2Click(Sender: TObject);
var Excel,libro:Variant;
i,i_det:Integer;
begin
   Excel:=CreateOleObject('Excel.Application');
   Excel.WorkBooks.Add (-4167);
   Excel.WorkBooks[1].WorkSheets[1].Name := 'Reporte';
   Libro := Excel.WorkBooks[1].WorkSheets['Reporte'];
   with Qry_resumen do
    begin
      First;
      i:=i+1;
      while not Qry_resumen.Eof do
        begin
          i:=i+1;
          Libro.Cells [1,1] := 'Licitación';
          Libro.Cells [1,2] := 'Zona';
          Libro.Cells [1,3] := 'Gpo';
          Libro.Cells [1,4] := 'Gen';
          Libro.Cells [1,5] := 'Esp';
          Libro.Cells [1,6] := 'dif';
          Libro.Cells [1,7] := 'Var';

          Libro.Cells [i,1] :=Qry_resumen.FieldByName ('licitacion').AsString;
          Libro.Cells [i,2] :=Qry_resumen.FieldByName ('zona').AsString;
          libro.Cells [i,3] :=Qry_resumen.FieldByName ('gpo').AsString;
          Libro.Cells [i,4] :=Qry_resumen.FieldByName ('gen').AsString;
          Libro.Cells [i,5] :=Qry_resumen.FieldByName ('esp').AsString;
          Libro.Cells [i,6] :=Qry_resumen.FieldByName ('dif').AsString;
          Libro.Cells [i,7] :=Qry_resumen.FieldByName ('var').AsString;
          with Qry_detalle do
            begin
             First;
             while not Qry_detalle.Eof do
              begin
               Libro.Cells [9,1] :=Qry_detalle.FieldByName ('licitante').AsString;
               Next;
              end;  
            end;
          Next;
        end;
          Excel.visible:=True; 
    end;

la consulta maestro tiene 4 registros y a su vez cada registro de maestro tiene 5 0 6 registros detalle lo que quiero en la hoja de excel seria

registro query maestro 1

y todos los registros detalle

celda en blanco

registro query maestro 2

y todos sus registros detalle


y asi hasta que termine con los registros de la consulta maestro.

alguna idea, de antemano muchas gracias

oscarac 19-09-2011 17:14:58

y exactamente cual es el problema?
no lo hace como tu quieres?
que esta mostrando?
muestra alguna cabecera, algun detalle?

me parece que el problema es que no estas chequeando bien tus begins y tus end

duilioisola 19-09-2011 18:12:07

Sin saber cual es tu problema creo haber visto un posible error:

Creo que te falta incrementar la variable i cuando recorres el detalle.
Código Delphi [-]
procedure TForm_requerimiento.btn2Click(Sender: TObject);
var Excel,libro:Variant;
i,i_det:Integer;
begin
   Excel:=CreateOleObject('Excel.Application');
   Excel.WorkBooks.Add (-4167);
   Excel.WorkBooks[1].WorkSheets[1].Name := 'Reporte';
   Libro := Excel.WorkBooks[1].WorkSheets['Reporte'];

   // Esto se escribe en la primera linea
   // Son los titulos
   i := 1;
   Libro.Cells [i,1] := 'Licitación';
   Libro.Cells [i,2] := 'Zona';
   Libro.Cells [i,3] := 'Gpo';
   Libro.Cells [i,4] := 'Gen';
   Libro.Cells [i,5] := 'Esp';
   Libro.Cells [i,6] := 'dif';
   Libro.Cells [i,7] := 'Var';
   with Qry_resumen do
    begin
      First;
      while not Qry_resumen.Eof do
        begin
          // Incremente el numero de linea antes de escribir estos datos
          // Son los datos de cabecera
          i:=i+1;
          Libro.Cells [i,1] :=Qry_resumen.FieldByName ('licitacion').AsString;
          Libro.Cells [i,2] :=Qry_resumen.FieldByName ('zona').AsString;
          libro.Cells [i,3] :=Qry_resumen.FieldByName ('gpo').AsString;
          Libro.Cells [i,4] :=Qry_resumen.FieldByName ('gen').AsString;
          Libro.Cells [i,5] :=Qry_resumen.FieldByName ('esp').AsString;
          Libro.Cells [i,6] :=Qry_resumen.FieldByName ('dif').AsString;
          Libro.Cells [i,7] :=Qry_resumen.FieldByName ('var').AsString;
          with Qry_detalle do
            begin
             First;
             while not Qry_detalle.Eof do
              begin
               // Incremente el numero de linea antes de escribir cada linea de detalle
               // Son los datos de detalle
               i:=i+1;
               Libro.Cells [i,1] :=Qry_detalle.FieldByName ('licitante').AsString;
               Next;
              end;  
            end;
          Next;
        end;
          Excel.visible:=True; 
    end;

microbiano 19-09-2011 22:35:20

gracias amigo afortunadamen tenias razon el problema era el que muestras que no recorria todo el detalle ahora la pregunta es la siguiente
el dato que tengo guardaro en la base de datos es 060, sin embargo al exportarlo po pone solo 60, alguna sugerencia para ello?

duilioisola 19-09-2011 22:40:08

Eso es porque Excel es muuuuy inteligente y cree que pones un número.
Si es un string, prueba a poner una comilla simle ' antes.
Código Delphi [-]
while not Qry_detalle.Eof do
              begin
               // Incremente el numero de linea antes de escribir cada linea de detalle
               // Son los datos de detalle
               i:=i+1;
               Libro.Cells [i,1] := '''' + Qry_detalle.FieldByName ('licitante').AsString;
               Next;
              end;

Tembién puedes probar entrecomillando todo el string:

Código Delphi [-]
while not Qry_detalle.Eof do
              begin
               // Incremente el numero de linea antes de escribir cada linea de detalle
               // Son los datos de detalle
               i:=i+1;
               Libro.Cells [i,1] := '"' + Qry_detalle.FieldByName ('licitante').AsString + '"';
               Next;
              end;

oscarac 19-09-2011 22:49:22

tambien fijate en esta opcion

Código Delphi [-]
 
WorkSheet.cells[i, 1].NumberFormat := '@';
para que el dato lo considere como caracter


La franja horaria es GMT +2. Ahora son las 03:15:59.

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