Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Exportar bd e imagen a excel desde Delphi7 (https://www.clubdelphi.com/foros/showthread.php?t=87543)

jeremiselxi 20-01-2015 21:21:43

Exportar bd e imagen a excel desde Delphi7
 
Buenas tardes Colegas.

Estoy presentando la siguiente situación. Necesito exportar a Excel una BD y esto lo hago con el siguiente código:

Código Delphi [-]

procedure TForm1.ExportaraexcelClick(Sender: TObject);
var
  Libro : _WORKBOOK;
  Hoja  : _WORKSHEET;
  i,colora:Integer;
  FileImage : String;
begin

  FileImage := ExtractFilePath(Application.ExeName)+'\Chart.bmp';

i:=0;
colora:=1;



Libro := Excel.Workbooks.Add(Null, 0);
Hoja  := Libro.Sheets[1] as _WORKSHEET;


//titulo
i:=i+1;


        if colora=1 then
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.Color :=$00F1E6DC ;
          colora:= colora +1;
        end
        else
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.ColorIndex :=2 ;
          colora:= colora -1;
        end;


Hoja.Cells.Item[i,1]:='Totales';
Hoja.Cells.Item[i,2]:='Actividades Realizadas';



//Insertamos todos los registros al excel  y en dbgrid 1
  with table do
    begin
    first;
      while not EOF Do
        begin
        i:=i+1;
        Hoja.Cells.Item[i,1]:=DBGrid1.Fields[0].AsString;
        Hoja.Cells.Item[i,2]:=DBGrid1.Fields[1].AsString;

        if colora=1 then
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.Color :=$00F1E6DC ;
          colora:= colora +1;
        end
        else
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.ColorIndex :=2 ;
          colora:= colora -1;
        end;

//        Hoja.Cells.Item[i,3]:=DBGrid1.Fields[2].AsString;
        Next;
   end;//while
end;//with





//Luego insertamos el total

        i:=i+1;
        Hoja.Cells.Item[i,1]:= 'Total ';
//      Hoja.Cells.Item[i,2]:= total.Caption;


Excel.Visible[0] := true;

ShowMessage('Exportado con Exito!');

end;

Ahora bien, para exportar una imagen a Excel lo hago de la siguiente manera:

Código Delphi [-]
uses ComObj;

procedure TForm1.exportargraficoaexcelClick(Sender: TObject);
var excel1: OleVariant;
begin
 excel1 := CreateOleObject('Excel.Application');
 excel1.Workbooks.add.worksheets[1].shapes.addpicture(ExtractFilePath(Application.ExeName)+'\Chart.bmp', False, True, 6, 6, 666, 666);
 excel1.visible := True
end;

pero por mas que he tratado me salen en libros diferentes y necesito que salga en uno solo.

Lo intenté de esta manera y aún así me salen en libros diferentes.

Código Delphi [-]
procedure TForm1.ExportaraexcelClick(Sender: TObject);
var
  Libro : _WORKBOOK;
  Hoja  : _WORKSHEET;
  i,colora:Integer;
  FileImage : String;
  var excel1: OleVariant;
begin

  FileImage := ExtractFilePath(Application.ExeName)+'\Chart.bmp';

i:=0;
colora:=1;



Libro := Excel.Workbooks.Add(Null, 0);
Hoja  := Libro.Sheets[1] as _WORKSHEET;


//titulo
i:=i+1;


        if colora=1 then
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.Color :=$00F1E6DC ;
          colora:= colora +1;
        end
        else
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.ColorIndex :=2 ;
          colora:= colora -1;
        end;


Hoja.Cells.Item[i,1]:='Totales';
Hoja.Cells.Item[i,2]:='Actividades Realizadas';



//Insertamos todos los registros al excel  y en dbgrid 1
  with table do
    begin
    first;
      while not EOF Do
        begin
        i:=i+1;
        Hoja.Cells.Item[i,1]:=DBGrid1.Fields[0].AsString;
        Hoja.Cells.Item[i,2]:=DBGrid1.Fields[1].AsString;

        if colora=1 then
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.Color :=$00F1E6DC ;
          colora:= colora +1;
        end
        else
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.ColorIndex :=2 ;
          colora:= colora -1;
        end;

//        Hoja.Cells.Item[i,3]:=DBGrid1.Fields[2].AsString;
        Next;
   end;//while
end;//with





//Luego insertamos el total

        i:=i+1;
        Hoja.Cells.Item[i,1]:= 'Total ';
//      Hoja.Cells.Item[i,2]:= total.Caption;


//Excel.Visible[0] := true;

 excel1 := GetActiveOleObject('Excel.Application');
 excel1.Workbooks.add.worksheets[1].shapes.addpicture(ExtractFilePath(Application.ExeName)+'\Chart.bmp', False, True, 6, 6, 666, 666);
 excel1.visible := True;


ShowMessage('Exportado con Exito!');

end;

Gracias de antemano.

Saludos

jeremiselxi 20-01-2015 21:41:20

Me respondo yo mismo:

Adjunto el código:

Código Delphi [-]

procedure TForm1.ExportaraexcelClick(Sender: TObject);
var
  Libro : _WORKBOOK;
  Hoja  : _WORKSHEET;
  i,colora:Integer;
  FileImage : String;
  var excel1: OleVariant;
begin

  FileImage := ExtractFilePath(Application.ExeName)+'\Chart.bmp';

i:=0;
colora:=1;



Libro := Excel.Workbooks.Add(Null, 0);
Hoja  := Libro.Sheets[1] as _WORKSHEET;


//titulo
i:=i+1;


        if colora=1 then
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.Color :=$00F1E6DC ;
          colora:= colora +1;
        end
        else
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.ColorIndex :=2 ;
          colora:= colora -1;
        end;


Hoja.Cells.Item[i,1]:='Totales';
Hoja.Cells.Item[i,2]:='Actividades Realizadas';



//Insertamos todos los registros al excel  y en dbgrid 1
  with table do
    begin
    first;
      while not EOF Do
        begin
        i:=i+1;
        Hoja.Cells.Item[i,1]:=DBGrid1.Fields[0].AsString;
        Hoja.Cells.Item[i,2]:=DBGrid1.Fields[1].AsString;

        if colora=1 then
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.Color :=$00F1E6DC ;
          colora:= colora +1;
        end
        else
        begin
          Hoja.Range['A'+ inttostr(i),'B'+ inttostr(i)].Interior.ColorIndex :=2 ;
          colora:= colora -1;
        end;

//        Hoja.Cells.Item[i,3]:=DBGrid1.Fields[2].AsString;
        Next;
   end;//while
end;//with





//Luego insertamos el total

        i:=i+1;
        Hoja.Cells.Item[i,1]:= 'Total ';
//      Hoja.Cells.Item[i,2]:= total.Caption;


//Excel.Visible[0] := true;

 excel1 := GetActiveOleObject('Excel.Application');

 excel1.ActiveSheet.Cells[3, 3].Select;
 excel1.ActiveSheet.Pictures.Insert(FileImage);

 excel1.visible := True;


ShowMessage('Exportado con Exito!');

end;


La franja horaria es GMT +2. Ahora son las 04:53:48.

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