PDA

Ver la Versión Completa : Encuentro código en internet sobre excel... Alguien puede mejorarlo...


GustavoCruz
22-09-2007, 19:34:51
Prueba con esta funcion que escribio uno de los muchos gurus de 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
Prueba con esta funcion que escribio uno de los muchos gurus de 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
Prueba con esta funcion que escribio uno de los muchos gurus de delphi



procedure SendToExcel(aDataSet: TDataSet);
var
xFile:TextFile;
cFile:String;
I, Row: Integer;
begin
cFile := 'reporte.xls';
AssignFile(xFile,cFile);
writeln(xFile,'<table border=1>');
writeln(xFile,'<tr>');

for I := 0 to aDataSet.Fields.Count - 1 do
begin

RangeE.Value := aDataSet.Fields[I].DisplayLabel;
RangeE := RangeE.Next;
end;
writeln(xFile,'</tr>');

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;

writeln(xFile,'</table>');
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..



procedure SendToExcelFromHTML(aDataSet: TDataSet);
var
xFile:TextFile;
cFile:String;
I, Row: Integer;
begin
cFile := 'reporte.xls';
AssignFile(xFile,cFile);
writeln(xFile,'<table border=1>');
writeln(xFile,'<th>');

for I := 0 to aDataSet.Fields.Count - 1 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 I := 0 to aDataSet.Fields.Count - 1 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>