Ver Mensaje Individual
  #7  
Antiguo 06-07-2017
Avatar de roman
roman roman is offline
Moderador
 
Registrado: may 2003
Ubicación: Ciudad de México
Posts: 20.269
Reputación: 10
roman Es un diamante en brutoroman Es un diamante en brutoroman Es un diamante en bruto
No tengo Delphi a mano y mi fluidez en el lenguaje ya no es lo que era, pero aquí una base de la cual partir:

Código Delphi [-]
uses ComObj, Clipbrd;

(*
  Exporta el contenido de un DataSet a un archivo de Excel. Desde luego, Excel debe estar instalado
  en el sistema y el archivo resultante estará en el formato de la versión instalada.
*)
procedure Exportar(DataSet: TDataSet; Archivo: String);
var
  Excel, Libro: OleVariant;
  CSV: TStrings;
  Linea: String;

begin
  CSV := TStringList.Create;

  try
    DataSet.First;

    while not DataSet.Eof do
    begin
      Linea := '';

      for I := 0 to DataSet.Fields.Count - 1 do
        Linea := Linea + '"' + DataSet.Fields[i].AsString + '"' + #9;

      CSV.Add(Linea);
      DataSet.Next;
    end;

    Clipboard.AsText := CSV.Text;

    Excel := CreateOleObject('Excel.Application');
    Excel.SheetsInNewWorkbook := 1;
    Libro := Excel.WorkBooks.Add;
    Libro.ActiveSheet.Paste();
    Libro.SaveAs(Archivo);
  finally
    Excel.Quit;
    CSV.Free;
  end;
end;

LineComment Saludos
Responder Con Cita