导出到Excel表格:
procedure Tf_Normal.ExportToExcel(DbGrid: TDBGrid; Query: TAdoQuery;ExcelApp:variant);
var
i,j,FieldNum:integer;
begin
with Query do
begin
DisableControls;
fieldNum := dbgrid.fieldCount;
for i:=1 to fieldNum do //写表头
begin
ExcelApp.Cells[1,i]:=Fields[i-1].FieldName;
end;
first;
i:=2;
while not eof do
begin
for j:=1 to fieldNum do
begin
ExcelApp.Cells[i,j]:=fields[j-1].AsString;
end;
inc(i);
if (i mod 20)=0 then
ExcelApp.Cells[i+10,1].Activate;
next;
end;
EnableControls;
end;
end;
procedure Tf_Normal.ButtonClick(Sender: TObject);
var
ExcelApp: variant;
begin
application.ProcessMessages;
try
ExcelApp:=createoleobject('Excel.application');
except
messageDlg('请先安装MicroSoft Excel',mtError,[mbok],0);
exit;
end;
ExcelApp.Visible := True;
ExcelApp.Caption := '名字';
ExcelApp.WorkBooks.Add;
ExcelApp.WorkSheets[1].Activate;
ExcelApp.WorkSheets[1].name:='表名';
ExcelApp.ActiveSheet.Rows[1].Font.Bold:= True;
ExcelApp.Columns[1].NumberFormatLocal:='@';
ExportToExcel(DbGrid: TDBGrid; Query: TAdoQuery;ExcelApp:variant);
ExcelApp.WorkBooks.Close;
ExcelApp.Quit;
end;