给你一段代码,自己看看吧。
procedure TFrmbbfxreport.SBexportdataClick(Sender: TObject);
var
i,row:integer;
begin
if SaveDialog1.Execute then
begin
Screen.Cursor:=crHourGlass;
ExcelApplication1.Connect;
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
if not cwxtdm.atbltemp.Active then
begin
cwxtdm.atbltemp.Open;
end;
for i:=0 to cwxtdm.atbltemp.Fields.Count-1 do
ExcelWOrkSheet1.Cells.Item[1,i+1]:=cwxtdm.atbltemp.Fields.FieldName;
row:=2;
while not cwxtdm.atbltemp.Eof do
begin
for i:=0 to cwxtdm.atbltemp.Fields.Count-1 do
begin
ExcelWOrkSheet1.Cells.Item[row,i+1]:=cwxtdm.atbltemp.Fields.AsString;
end;
row:=row+1;
cwxtdm.atbltemp.Next;
end;
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName);
ExcelWOrkBook1.Close(false);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Screen.Cursor:=crDefault;
Application.MessageBox('导出成功!','提示',0);
end;
end;