PDA

Ver la Versión Completa : exportar excel


luis77
29-08-2015, 17:22:06
hola buen dia espero alguien me pueda ayudar se los agraderia
tengo el siguiente codigo el cual si me realiza la exportacion
a excel.
el detalle esta en que cuan ejecuto la aplicacion solo le doy click si me extrae los datos
pero cuando quiero volver a dar click ya no .
que me estar faltando.
gracias saludos

procedure TForm1.Button1Click(Sender: TObject);

var i:integer;
PLANILHA, Excel,libro: VARIANT;
LINHA,CONT:INTEGER;
begin
//CONT:=Query.RecordCount;
//ProgressBar1.Max:=CONT;
//ProgressBar1.Position:=0; QUERY.Filtered:=FALSE;
LINHA:=1;
PLANILHA:=CreateOleObject('Excel.Application');
PLANILHA.caption:='Delphi para excel';
PLANILHA.visible:=true;
PLANILHA.workbooks.add(-4167);
//PLANILHA.workbooks[1].WorkSheets[1].name:='Reporte';
//linha:=PLANILHA.WorkBooks[1].WorkSheets['Reporte'];
PLANILHA.cells[1,1]:='Sucursal';
PLANILHA.cells[1,2]:='TipoPrenda';
PLANILHA.cells[1,3]:='Contratos';
PLANILHA.cells[1,4]:='Estatusprenda';
PLANILHA.cells[1,5]:='Descripcion';
PLANILHA.cells[1,6]:='Partida';
PLANILHA.cells[1,7]:='Kilataje';
PLANILHA.cells[1,8]:='Peso';
PLANILHA.cells[1,9]:='PesoNeto';
PLANILHA.cells[1,10]:='ImporteAvaluo';
PLANILHA.cells[1,11]:='ImporteOriginal';
PLANILHA.cells[1,12]:='AbonoAcapital';
PLANILHA.cells[1,13]:='ImportePrestamo';
PLANILHA.cells[1,14]:='FechaContrato';
PLANILHA.cells[1,15]:='Fechavencimiento';


query.DisableControls;

try while not query.Eof do
begin
PLANILHA.cells[linha,1]:=query.FieldByName('Sucursal').AsString;
PLANILHA.cells[linha,2]:=Query.FieldByName('TipoPrenda').AsString;
PLANILHA.cells[linha,3]:=Query.FieldByName('Contratos').AsString;
PLANILHA.cells[linha,4]:=Query.FieldByName('EstatusPrenda').AsString;
PLANILHA.cells[linha,5]:=Query.FieldByName('Descripcion').AsString;
PLANILHA.cells[linha,6]:=Query.FieldByName('Partida').AsString;
PLANILHA.cells[linha,7]:=Query.FieldByName('Kilataje').AsString;
PLANILHA.cells[linha,8]:=Query.FieldByName('Peso').AsString;
PLANILHA.cells[linha,9]:=Query.FieldByName('PesoNeto').AsString;
PLANILHA.cells[linha,10]:=Query.FieldByName('ImporteAvaluo').AsString;
PLANILHA.cells[linha,11]:=Query.FieldByName('ImporteOriginal').AsString;
PLANILHA.cells[linha,12]:=Query.FieldByName('AbonoACapital').AsString;
PLANILHA.cells[linha,13]:=Query.FieldByName('ImportePrestamo').AsString;
PLANILHA.cells[linha,14]:=Query.FieldByName('FechaContrato').AsString;
PLANILHA.cells[linha,15]:=Query.FieldByName('FechaVencimiento').AsString;

LINHA:=linha+1;
query.Next;
// ProgressBar1.Position:=ProgressBar1.Position+1;
end;
PLANILHA.columns.autofit;
planilha.Visible:=true;
finally
Query.EnableControls;
PLANILHA:=Unassigned;
query.Next;
QUERY.ClearFields;
end;


end;

end.

Casimiro Notevi
29-08-2015, 18:31:37
Recuerda poner los tags al código fuente, ejemplo:

http://www.clubdelphi.com/images/UtilizarTAGs.png

Gracias :)

ecfisa
29-08-2015, 21:29:04
Hola Luis.
...
el detalle esta en que cuan ejecuto la aplicacion solo le doy click si me extrae los datos
pero cuando quiero volver a dar click ya no .
...

Creo haber visto algo que deberías revisar... Cuando termina el método Button1Click, la tabla fué recorrida desde la primera a la última fila, quedando esta como la actual fila activa.
En la próxima llamada a Button1Click el ciclo while no se ejecutará ya que la condición de salida está satisfecha en la comprobación,
while not query.Eof do

Para reproducir nuevamente el comportamiento del método Button1Click, tenes que fijar el primer registro de la tabla como la fila activa antes de entrar al ciclo while:

...
PLANILHA.cells[1,13]:='ImportePrestamo';
PLANILHA.cells[1,14]:='FechaContrato';
PLANILHA.cells[1,15]:='Fechavencimiento';

query.DisableControls;
query.First; // <== Aquí
try
while not query.Eof do
...


Saludos :)

luis77
31-08-2015, 19:16:08
gracias eficsa eso era muy amable disculpa es muy bajo mi nivel
saludos:D