Title: sql data to excel
Question: how to sql data to excel
Answer:
procedure Trydc.suiButton2Click(Sender: TObject);
var
i,row,column:integer;
begin
Try
ExcelApplication1.Connect;
Except
MessageDlg('Excel may not be installed',mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.Caption:='Excel Application';
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
ADOQuery1.Open;
row:=1;
While Not(ADOQuery1.Eof) do
begin
column:=1;
for i:=1 to ADOQuery1.FieldCount do
begin
ExcelWorksheet1.Cells.Item[row,column]:=ADOQuery1.fields[i-1].AsString;
column:=column+1;
end;
ADOQuery1.Next;
row:=row+1;
end;
end;