給你代碼,先添加一個ADOQuery,和EXCEL控件后,使用下列代碼,即可:
procedure TForm1.BitBtn3Click(Sender: TObject);
var
i,j:integer;
begin
try
ExcelApplication1.Connect; //連接EXCEL
ExcelApplication1.Visible[0] := True; //顯示EXCEL界面
ExcelApplication1.Workbooks.Add(NULL,0); //新增一個工作表
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); //連接到工作薄1
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets[1] as _WorkSheet); //選擇工作表1
for j:=1 to ADOQuery1.RecordCount-1 do
begin
for i:=0 to ADOQuery1.FieldCount-1 do
begin
if ADOQuery1.Fields.IsNull then
ExcelApplication1.Cells.Item[j,i+1].Value
:= ' '
else
ExcelApplication1.Cells.Item[j,i+1].Value
:= string(ADOQuery1.Fields.Value);
end;
ADOQuery1.Next;
end;
except
ExcelApplication1.Quit;
ExcelApplication1.Disconnect; //關閉EXCEL的控制
end;
ExcelApplication1.Disconnect; //關閉EXCEL的控制
end;