我用的是bitbtn控件, 请在窗体上加上bitbtn,ttable,tdatabase,dbgrid
ExcelApplication,ExcelWorksheet, ExcelWorkbook1:控件
procedure TForm6.BitBtn1Click(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);
Table1.Open;
row:=1;
While Not(Table1.Eof)do
begin
column:=1;
for i:=1 to Table1.FieldCountdo
begin
ExcelWorksheet1.Cells.Item[row,column]:=Table1.fields[i-1].AsString;
column:=column+1;
end;
Table1.Next;
row:=row+1;
end;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
end;