我有一小流程,你参考参考。
procedure TForm1.Button1Click(Sender: TObject);
var
fieldcounts,j,i:integer;
begin
excelapplication1.Connect ;
excelapplication1.Workbooks.Add(null,0);
excelworkbook1.ConnectTo(excelapplication1.Workbooks[1]);
excelworksheet1.ConnectTo(excelworkbook1.Sheets[1] as _worksheet);
fieldcounts:=dbgrid1.FieldCount;
For j:=0 to FieldCounts-1do
excelworksheet1.Cells.Item[1,j+1]:=dbgrid1.Fields[j].FieldName;
i:=2;
adoquery1.First;
while not adoquery1.Eofdo
begin
For j:=0 to FieldCounts-1do
excelworksheet1.Cells.Item[i,j+1]:=adoquery1.Fields[j].Value;
i:=i+1;
adoquery1.Next;
end;
if savedialog1.Execute then
begin
adoquery1.First;
excelworksheet1.SaveAs(savedialog1.FileName);
excelworkbook1.Close;
excelapplication1.Quit ;
end;
adoquery1.First;
end;