Passar para o Excel
Como que eu faço p/ passar para o Excel, uma consulta de uma query?
Agradeço
Agradeço
Coppola
Curtidas 0
Respostas
Joilson_gouveia
01/10/2003
Estude a dica abaixo:
uses
ComObj, ActiveX, Excel2000; // ou Excel97...
procedure SendToExcel(aDataSet: TDataSet);
var
PreviewToExcel: TExcelApplication;
RangeE: Excel2000.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
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;
No evento OnClick do botão coloque o código abaixo:
procedure TForm1.Button1Click(Sender: TObject);
begin
SendToExcel(Table1);
end;
uses
ComObj, ActiveX, Excel2000; // ou Excel97...
procedure SendToExcel(aDataSet: TDataSet);
var
PreviewToExcel: TExcelApplication;
RangeE: Excel2000.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
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;
No evento OnClick do botão coloque o código abaixo:
procedure TForm1.Button1Click(Sender: TObject);
begin
SendToExcel(Table1);
end;
GOSTEI 0