Incluir ComObj no uses
procedure TFormFiltro.BBExportarClick(Sender: TObject);
var CamposExcel, CamposDelphi, linha: integer;
excel: variant;
begin
if SaveDialog1.Execute then
begin
try
Excel:= CreateOleObject('Excel.Application');
Excel.Workbooks.add(1);
Excel.Workbooks[1].SaveAs(SaveDialog1.FileName);
except
Application.MessageBox ('Versão do Ms-Excel'+
'Incompatível','Erro',MB_OK+MB_ICONEXCLAMATION);
exit;
end;
with TFormProgresso.Create(nil) do //Para criar um form para mostrar o processamento
try
ProgressBar1.Max := DMAvarias.QyFiltro.RecordCount;
Show; // show a splash screen contain ProgressBar control
Update; // force display of Form5
begin
for CamposDelphi:= 1 to DMAvarias.QyFiltro.FieldCount - 1 do
begin
Excel.Cells[1,CamposDelphi]:= DMAvarias.QyFiltro.Fields[CamposDelphi].DisplayName;
end;
Linha := 2;
DMAvarias.QyFiltro.First;
while not DMAvarias.QyFiltro.Eof do
begin
for CamposExcel:= 1 to DMAvarias.QyFiltro.FieldCount - 1 do
begin
Excel.Cells[Linha,CamposExcel]:= DMAvarias.QyFiltro.Fields[CamposExcel].AsString;
end;
Inc(Linha);
DMAvarias.QyFiltro.Next;
ProgressBar1.StepBy(1);
end;
Excel.Columns.AutoFit;
Excel.WorkBooks[1].Save;
Excel.WorkBooks[1].Close;
end;
finally
Free;
end;
end;
end;