Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Servers (https://www.clubdelphi.com/foros/forumdisplay.php?f=9)
-   -   Encuentro código en internet sobre excel... Alguien puede mejorarlo... (https://www.clubdelphi.com/foros/showthread.php?t=48361)

GustavoCruz 22-09-2007 19:34:51

Encuentro código en internet sobre excel... Alguien puede mejorarlo...
 
Prueba con esta funcion que escribio uno de los muchos gurus de delphi


Código Delphi [-]
 
procedure SendToExcel(aDataSet: TDataSet); 
var 
  PreviewToExcel: TExcelApplication; 
  RangeE: Excel2000.ExcelRange; //or RangeE: Excel97.Range 
  I, Row: Integer; 
  Bookmark: TBookmarkStr; 
begin 
  PreviewToExcel := TExcelApplication.Create(Self); 
  PreviewToExcel.Connect; 
  PreviewToExcel.Workbooks.Add(NULL, 0); 
  RangeE := PreviewToExcel.ActiveCell; 
  for I := 0 to aDataSet.Fields.Count - 1 do 
  begin 
    RangeE.Value := aDataSet.Fields[i].DisplayLabel; 
    RangeE := RangeE.Next; 
  end; 
  aDataSet.DisableControls; 
  try 
    Bookmark := aDataSet.Bookmark; 
    try 
      aDataSet.First; 
      Row := 2; 
      while not aDataSet.EOF do 
      begin 
        //Write down Record As Row in msExcel 
        RangeE := PreviewToExcel.Range['A' + IntToStr(Row), 'A' + IntToStr(Row)]; 
        for I := 0 to aDataSet.Fields.Count - 1 do 
        begin 
          RangeE.Value := aDataSet.Fields[i].AsString; 
          RangeE := RangeE.Next; 
        end; 
        aDataSet.Next; 
        Inc(Row); 
      end; 
    finally 
      aDataSet.Bookmark := Bookmark; 
    end; 
  finally 
    aDataSet.EnableControls; 
  end; 
  RangeE := PreviewToExcel.Range['A1', chr(64 + aDataSet.Fields.Count) + 
IntToStr(Row - 1)]; 
  RangeE.AutoFormat(8, NULL, NULL, NULL, NULL, NULL, NULL); 
  PreviewToExcel.Visible[0] := True; 
  PreviewToExcel.Disconnect; 
end;

La cuestión es que al implementarlo demora mucho para mostrar el resultado, esto cuando el dataset es grande

pcicom 22-09-2007 22:30:09

Cita:

Empezado por GustavoCruz (Mensaje 233160)
Prueba con esta funcion que escribio uno de los muchos gurus de delphi


Código Delphi [-]
 
procedure SendToExcel(aDataSet: TDataSet); 
var 
  PreviewToExcel: TExcelApplication; 
  RangeE: Excel2000.ExcelRange; //or RangeE: Excel97.Range 
  I, Row: Integer; 
  Bookmark: TBookmarkStr; 
begin 
  PreviewToExcel := TExcelApplication.Create(Self); 
  PreviewToExcel.Connect; 
  PreviewToExcel.Workbooks.Add(NULL, 0); 
  RangeE := PreviewToExcel.ActiveCell; 
  for I := 0 to aDataSet.Fields.Count - 1 do 
  begin 
    RangeE.Value := aDataSet.Fields[i].DisplayLabel; 
    RangeE := RangeE.Next; 
    Application.processmessages;

  end; 
  aDataSet.DisableControls; 
  try 
    Bookmark := aDataSet.Bookmark; 
    try 
      aDataSet.First; 
      Row := 2; 
      while not aDataSet.EOF do 
      begin 
    Application.processmessages;

        //Write down Record As Row in msExcel 
        RangeE := PreviewToExcel.Range['A' + IntToStr(Row), 'A' + IntToStr(Row)]; 
        for I := 0 to aDataSet.Fields.Count - 1 do 
        begin 
          RangeE.Value := aDataSet.Fields[i].AsString; 
          RangeE := RangeE.Next; 
    Application.processmessages;

        end; 
        aDataSet.Next; 

        Inc(Row); 
      end; 
    finally 
    Application.processmessages;

      aDataSet.Bookmark := Bookmark; 

    end; 
  finally 
    aDataSet.EnableControls; 
  end; 
    Application.processmessages;

  RangeE := PreviewToExcel.Range['A1', chr(64 + aDataSet.Fields.Count) + 
IntToStr(Row - 1)]; 
    Application.processmessages;

  RangeE.AutoFormat(8, NULL, NULL, NULL, NULL, NULL, NULL); 
    Application.processmessages;

  PreviewToExcel.Visible[0] := True; 
  PreviewToExcel.Disconnect; 
end;

La cuestión es que al implementarlo demora mucho para mostrar el resultado, esto cuando el dataset es grande


Puedes Agregar entre el CODIGO instrucciones:

Application.processmessages;

Y Veras que se acelerara la presntacion..

GustavoCruz 22-09-2007 23:18:16

Muchas gracias pcicom, pero el problema continua, los dataset que se generan superan los 2800 registros..., pero en todo caso gracias; ya por lo menos la aplicación no se queda paralizada.:)

Lepe 23-09-2007 13:38:02

Cuantos más Application.ProcessMessages pongas... más se ralentizará los bucles.

Es cierto que tu interfaz, responderá de forma más ágil, podrás mover el ratón, pulsar botones, etc..., pero el tiempo total de espera se incrementa notablemente.

No he trabajado con automatización OLE, pero si el código original no tiene ningun processMessages, es que no son necesarios.

El primero que aparece sobra. ¿Cuantos campos tendrá un registro? 10 como mucho?, no hace falta una pausa por cada uno de ellos.

Yo solo dejaría 2 Processmessages, el segundo y el último.

Lo que sí podrías añadir es un label diciendo "Exportando registro 3 de 3000" ... por ejemplo.

Saludos

pcicom 23-09-2007 14:48:09

Cita:

Empezado por GustavoCruz (Mensaje 233160)
Prueba con esta funcion que escribio uno de los muchos gurus de delphi


Código Delphi [-]
 
procedure SendToExcel(aDataSet: TDataSet); 
var
  xFile:TextFile;
  cFile:String; 
  I, Row: Integer; 
begin 
  cFile := 'reporte.xls';
  AssignFile(xFile,cFile);
  writeln(xFile,'');
  writeln(xFile,'');

  for I := 0 to aDataSet.Fields.Count - 1 dobegin 
         
    RangeE.Value := aDataSet.Fields[i].DisplayLabel; 
    RangeE := RangeE.Next; 
  end;
  writeln(xFile,'');
 
  aDataSet.DisableControls; 
  try 
    Bookmark := aDataSet.Bookmark; 
    try 
      aDataSet.First; 
      Row := 2; 
      whilenot aDataSet.EOF dobegin//Write down Record As Row in msExcel 
        RangeE := PreviewToExcel.Range['A' + IntToStr(Row), 'A' + IntToStr(Row)]; 
        for I := 0 to aDataSet.Fields.Count - 1 dobegin 
          RangeE.Value := aDataSet.Fields[i].AsString; 
          RangeE := RangeE.Next; 
        end; 
        aDataSet.Next; 
        Inc(Row); 
      end; 
    finally 
      aDataSet.Bookmark := Bookmark; 
    end; 
  finally 
    aDataSet.EnableControls; 
  end; 
  RangeE := PreviewToExcel.Range['A1', chr(64 + aDataSet.Fields.Count) + 
IntToStr(Row - 1)]; 
  RangeE.AutoFormat(8, NULL, NULL, NULL, NULL, NULL, NULL); 
  PreviewToExcel.Visible[0] := True; 
  PreviewToExcel.Disconnect; 

  writeln(xFile,'
'
); close(xFile); end;

La cuestión es que al implementarlo demora mucho para mostrar el resultado, esto cuando el dataset es grande



OTRA FORMA es que CREES el XLS al vuelo como un archivo tipo HTML con extencin de EXCEL .xls, y despues lo abres..

pcicom 23-09-2007 14:52:17

OTRA FORMA es que CREES el XLS al vuelo como un archivo tipo HTML con extencin de EXCEL .xls, y despues lo abres..


Código PHP:

procedure SendToExcelFromHTML(aDataSetTDataSet); 
var
  
xFile:TextFile;
  
cFile:String
  
IRowInteger
begin 
  cFile 
:= 'reporte.xls';
  
AssignFile(xFile,cFile);
  
writeln(xFile,'<table border=1>');
  
writeln(xFile,'<th>');
  
  for 
:= 0 to aDataSet.Fields.Count do 
    
writeln(xFile,'<td>' aDataSet.Fields[i].DisplayLabel '</td>'); 

  
writeln(xFile,'</th>');
 
  try 
      
aDataSet.First
      while 
not aDataSet.EOF do 
      
begin
        Application
.processmessagess;
        
writeln(xFile,'<tr>');

        for 
:= 0 to aDataSet.Fields.Count do 
           
writeln(xFile,'<td>' aDataSet.Fields[i].AsString '</td>'); 
        
aDataSet.Next
        
writeln(xFile,'</tr>');
        
Inc(Row); 
      
end
  
finally 

  end


  
writeln(xFile,'</table>');
  
closeFile(xFile);
 
  
ShellExecute(Form1.Handle,nil,PChar(cFile),'','',SW_SHOWNORMAL);   
  
end


La cuestión es que al implementarlo demora mucho para mostrar el resultado, esto cuando el dataset es grande[/quote]

<table>


La franja horaria es GMT +2. Ahora son las 03:47:38.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi