5,386
社区成员
发帖
与我相关
我的任务
分享
Procedure TFrmPubDbToExcel.DbToExcel(aHandle:TComponent;dbset:TDataSet);
var
Excel,WrkBook,WrkSheet:oleVariant;
SaveDialog1:TSaveDialog;
tmp,Row,SheetCount:integer;
begin
try
Excel:=CreateOleObject('Excel.Application');
Except
if Application.MessageBox('对不起,你的机器没有安装Microsoft Excel,是否继续导出?'
+ #13#13 + '导出后在您的机器上不能直接打开,必须安装Excel到机器上才能打开!',
'注意', MB_OKCANCEL) = ID_no then
exit;
end;
SaveDialog1:=TSaveDialog.Create(aHandle);
SaveDialog1.Filter:='*.xls|(*.xls Excel文件)';
try
if SaveDialog1.Execute then
begin
WrkBook:=Excel.WorkBooks.Add;
Row:=1;
SheetCount:=1;
dbSet.DisableControls;
dbSet.First;
ProBar.Max := dbSet.RecordCount;
ProBar.Position:=0;
while not dbSet.Eof do
begin
if Row=1 then
for tmp:=0 to dbSet.FieldCount-1 do
if dbSet.Fields.Fields[tmp].Visible then
Excel.WorkSheets[SheetCount].Cells[Row,tmp+1].Value:=dbSet.Fields.Fields[tmp].DisplayLabel;
inc(Row);
for tmp:= 0 to dbSet.FieldCount-1 do
begin
if dbSet.Fields[tmp].Visible then
Excel.WorkSheets[SheetCount].Cells[Row,Tmp+1].value:= '''' + dbSet.Fields[tmp].AsString;
end;
if Row>=50000 then
begin
SheetCount:=SheetCount+1;
Row:=0;
if SheetCount>3 then
begin
WrkSheet:=WrkBook.WorkSheets[WrkBook.WorkSheets.Count];
wrkBook.WorkSheets.Add(EmptyParam,WrkSheet,1,$FFFFEFB9);
end;
end;
ProBar.Position:=ProBar.Position+1;
dbSet.Next;
end;
Excel.ActiveWorkBook.SaveAs(SaveDialog1.FileName);
WrkBook.close;
Excel.Quit;
Excel:=unassigned;
Messagebox(self.Handle,Pchar('系统已经导出,请到《'+SaveDialog1.FileName+'》里查看!!!')
,'提示信息',MB_OK+MB_ICONINFORMATION);
end;
finally
SaveDialog1.Free;
dbSet.EnableControls;
end;
end;