用aSheet.Paste命令;由于数据量巨大,所以粘贴会花点时间,享用进度条显示其进程。
procedure Tbrowse.ExportToExcel(defaultName: string;
Grid: TDBGridEh);
var
lcid:integer;
var
ls_FileName:string;
I,K,M,N:integer;
y :integer;
tsList :TStringList;
s :string;
aSheet:Variant;
form_clip:Tform_clip;
begin
if not Grid.DataSource.DataSet.Active then // if 5
begin
Application.Messagebox('未与数据库连接!','消息',mb_OK+mb_IconStop);
Exit;
end; //end if 5
Grid.DataSource.DataSet.DisableControls;
//如果未装Excel,则退出。
try //try 30
Excel.Connect; // 打开Excel
Excel.Visible[0]:=false; // 显示Excel
Excel.Workbooks.Add(xlWBATWorksheet,0);
aSheet:=excel.Worksheets.Item[1];
except
Application.MessageBox('无法打开Xls文件,请确认已经安装EXCEL.','警告',mb_OK+mb_IconStop);
Exit;
end; //end try 30
Dlg_SaveToFile.FileName:=defaultName;
if not Dlg_SaveToFile.Execute Then Exit;
ls_FileName:=Dlg_SaveToFile.FileName;
try //try 15
K:=1;
lcid := LOCALE_USER_DEFAULT;
N:=Grid.Columns.count;
I:=Grid.DataSource.DataSet.RecordCount;
tsList:=TStringList.Create;
FormProgress:=TFormProgress.Create (self);
FormProgress.Show;
try
try
Grid.DataSource.DataSet.first;
while not Grid.DataSource.DataSet.Eof do
begin
s:='';
for y:=0 to n-1 do
begin
s:=s+Grid.DataSource.DataSet.Fields[y].AsString+#9;
Application.ProcessMessages;
end;
tsList.Add(s);
FormProgress.ProgressBar1.Position:=Trunc((K*100)/I);
FormProgress.Refresh;
INC(K);
Grid.DataSource.DataSet.next;
end;
finally
FormProgress.Hide;
FormProgress.free;
end;
Clipboard.AsText:=tsList.Text;
aSheet.Paste;
Excel.DisplayAlerts[lcid]:= false;
aSheet.Saveas(ls_FileName);
MessageBox(Application.Handle,'数据导出完毕!','系统提示',MB_ICONINFORMATION or MB_OK);
finally
tsList.Free;
Grid.DataSource.DataSet.EnableControls;
Excel.disconnect;
aSheet:=Unassigned; //释放VARIANT变量
end;
except
Application.Messagebox('数据导出错误!','消息',mb_OK+MB_ICONINFORMATION);
Exit;
end; //end try 80
procedure Tbrowse.ExportToExcel(defaultName: string;
Grid: TDBGridEh);
var
lcid:integer;
var
ls_FileName:string;
I,K,M,N:integer;
y :integer;
tsList :TStringList;
s :string;
aSheet:Variant;
form_clip:Tform_clip;
begin
if not Grid.DataSource.DataSet.Active then // if 5
begin
Application.Messagebox('未与数据库连接!','消息',mb_OK+mb_IconStop);
Exit;
end; //end if 5
Grid.DataSource.DataSet.DisableControls;
//如果未装Excel,则退出。
try //try 30
Excel.Connect; // 打开Excel
Excel.Visible[0]:=false; // 显示Excel
Excel.Workbooks.Add(xlWBATWorksheet,0);
aSheet:=excel.Worksheets.Item[1];
except
Application.MessageBox('无法打开Xls文件,请确认已经安装EXCEL.','警告',mb_OK+mb_IconStop);
Exit;
end; //end try 30
Dlg_SaveToFile.FileName:=defaultName;
if not Dlg_SaveToFile.Execute Then Exit;
ls_FileName:=Dlg_SaveToFile.FileName;
try //try 15
K:=1;
lcid := LOCALE_USER_DEFAULT;
N:=Grid.Columns.count;
I:=Grid.DataSource.DataSet.RecordCount;
tsList:=TStringList.Create;
FormProgress:=TFormProgress.Create (self);
FormProgress.Show;
try
try
Grid.DataSource.DataSet.first;
while not Grid.DataSource.DataSet.Eof do
begin
s:='';
for y:=0 to n-1 do
begin
s:=s+Grid.DataSource.DataSet.Fields[y].AsString+#9;
Application.ProcessMessages;
end;
tsList.Add(s);
FormProgress.ProgressBar1.Position:=Trunc((K*100)/I);
FormProgress.Refresh;
INC(K);
Grid.DataSource.DataSet.next;
end;
finally
FormProgress.Hide;
FormProgress.free;
end;
Clipboard.AsText:=tsList.Text;
aSheet.Paste;
Excel.DisplayAlerts[lcid]:= false;
aSheet.Saveas(ls_FileName);
MessageBox(Application.Handle,'数据导出完毕!','系统提示',MB_ICONINFORMATION or MB_OK);
finally
tsList.Free;
Grid.DataSource.DataSet.EnableControls;
Excel.disconnect;
aSheet:=Unassigned; //释放VARIANT变量
end;
except
Application.Messagebox('数据导出错误!','消息',mb_OK+MB_ICONINFORMATION);
Exit;
end; //end try 80
我觉得一次paste过去不如一条条记录直接传过去,你可以做个试验对比一下。
form*.show; //进度显示
application.ProcessMessages; //显示进度窗字体
结束的地方加入
application.ProcessMessages;
form3.close;form*中放个 Animate控件
unit*中Animate1.Active:=true;
这样就可以了
form3.show; //进度显示
application.ProcessMessages; //显示进度窗字体
结束的地方
application.ProcessMessages;
form3.close;form中加个Animatel 控件
unit中Animate1.Active:=true;
减少Application.ProcessMessages操作,可以在
if i mod 50 = 0 then Application.ProcessMessages;