Ver Mensaje Individual
  #4  
Antiguo 25-05-2005
Citlalli Citlalli is offline
No confirmado
 
Registrado: may 2005
Posts: 40
Reputación: 0
Citlalli Va por buen camino
pon un dbgrid en tu form, pon su propiedad visible en false, al query ponle un datasource y a este ultimo ligalo al dbgrid e intenta con este codigo

Código Delphi [-]
procedure Tfrm_Clientes.Exporta;
 var
  excel:Variant;
  fila, columna:Integer;
begin
  with qry_excel, qry_excel.SQL do
    begin
      Close;
      Open;
      if IsEmpty then
        begin
          MessageDlg('No existen datos que reportar!', mtWarning,[mbOk], 0);
          Exit;
        end;
      TRY
        excel := CreateOleObject('Excel.Application');
      EXCEPT
        ShowMessage('Excel no se pudo iniciar.');
        exit;
      END;
      excel.Visible := true;
      First;
      fila          := 1;
      excel.Workbooks.Add;
      //Poner los títulos
      for columna := 0 to DbGrid1.Columns.Count-1 do
        excel.Cells[fila,columna + 1].Value := DbGrid1.Columns[columna].Title.Caption;
      fila:=3;
      //Desengachar controles para agilizar
      DisableControls;
      TRY
        while not(eof) do
          begin
            for columna := 0 to DbGrid1.Columns.Count-1 do
              excel.Cells[fila,columna + 1].Value := FieldByName(DbGrid1.Columns[columna].FieldName).asString;
            Next;
            inc(fila);
          end;
      EXCEPT
        ShowMessage('Atención, se produjo un error en la transmisión.');
      END;
      EnableControls;
    end;
end;

necesitas poner el el uses de tu form "ComObj"

espero te sirva

saludos

Última edición por roman fecha: 04-08-2006 a las 17:06:47. Razón: agregar etiquetas [delphi] para mayor legibilidad
Responder Con Cita