procedure TForm1.Button1Click(Sender: TObject);
var
i,j,row,column:integer;
begin
Try
ExcelApplication1.Connect;
Except
MessageDlg('Excel 没有安装',mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=true;
ExcelApplication1.Caption:='Excel Not Istalled!';
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
query1.Open;
row:=1;
column:=1;
for j:= 0 to Query1.FieldCount-1 do
begin
ExcelWorksheet1.Cells.Item[row,column]:=query1.fields[j].FieldName;
column:=column+1;
end;
row:=2;
While Not(query1.Eof) do
begin
column:=1;
for i:=1 to query1.FieldCount do
begin
ExcelWorksheet1.Cells.Item[row,column]:=query1.fields[i-1].AsString;
column:=column+1;
end;
query1.Next;
row:=row+1;
end;
query1.first;
ExcelWorkbook1.Disconnect;
ExcelWorksheet1.Disconnect;
ExcelApplication1.DisConnect;
end;