关闭Excel的问题 ( 积分: 50 )

  • 主题发起人 主题发起人 bergkampzhu
  • 开始时间 开始时间
B

bergkampzhu

Unregistered / Unconfirmed
GUEST, unregistred user!
我用创建OLE对象的方法控制excel文件,对象名preview.操作完成后,执行了preview:=unassigned;可是进程中还有excel,占了8m内存.我想知道怎么么才能释放这个资源,或者处理完后干脆关掉所有excel进程,不知怎么办
代码如下.
try
preview:=createoleobject('excel.application');
except
application.MessageBox('无法打开预览,请确认是否正确安装Ecxel!','错误',MB_OK);
preview:=unassigned;
adotable3.Delete;
abort;
end;
try
preview.workbooks.open(gybpath+gyh+'.xls');
preview.workbooks[1].sheets[1].activate;
preview.DisplayAlerts:=false;
preview.workbooks[1].sheets[1].cells[58,11]:=zg_czy.xm;
preview.workbooks[1].saveas(gybpath+gyh);
except
application.MessageBox('签字失败!可能是工艺表Excel文件被破坏,或是网络问题.','错误',MB_OK);
preview:=unassigned;
adotable3.Delete;
abort;
end;
preview:=unassigned;
 
我用创建OLE对象的方法控制excel文件,对象名preview.操作完成后,执行了preview:=unassigned;可是进程中还有excel,占了8m内存.我想知道怎么么才能释放这个资源,或者处理完后干脆关掉所有excel进程,不知怎么办
代码如下.
try
preview:=createoleobject('excel.application');
except
application.MessageBox('无法打开预览,请确认是否正确安装Ecxel!','错误',MB_OK);
preview:=unassigned;
adotable3.Delete;
abort;
end;
try
preview.workbooks.open(gybpath+gyh+'.xls');
preview.workbooks[1].sheets[1].activate;
preview.DisplayAlerts:=false;
preview.workbooks[1].sheets[1].cells[58,11]:=zg_czy.xm;
preview.workbooks[1].saveas(gybpath+gyh);
except
application.MessageBox('签字失败!可能是工艺表Excel文件被破坏,或是网络问题.','错误',MB_OK);
preview:=unassigned;
adotable3.Delete;
abort;
end;
preview:=unassigned;
 
try
Excel.Quit;
except
on Exception do;
end;
 
try
preview:=createoleobject('excel.application');
except
application.MessageBox('无法打开预览,请确认是否正确安装Ecxel!','错误',MB_OK);
preview:=unassigned;
adotable3.Delete;
abort;
end;

try

try
preview.workbooks.open(gybpath+gyh+'.xls');
preview.workbooks[1].sheets[1].activate;
preview.DisplayAlerts:=false;
preview.workbooks[1].sheets[1].cells[58,11]:=zg_czy.xm;
preview.workbooks[1].saveas(gybpath+gyh);
except
application.MessageBox('签字失败!可能是工艺表Excel文件被破坏,或是网络问题.','错误',MB_OK);
preview:=unassigned;
adotable3.Delete;
abort;
end;

finally
preview.quit;
preview:=unassigned;
end;
 
ExcelApp.ActiveWorkBook.Save; //存如果必要
ExcelApp.WorkBooks.Close; //关
ExcelApp.Quit; //退出
ExcelApp.FREE; //必须释放进程
 
ExcelApp.Quit;
 
我写过一段代码,我的excel进程不会驻留内存,你可以试一试!

var
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
filename: string;
filename_xls: string;
i,j,ii,jj : integer;
confirm :word;
CurrentFile :word;
pathname :string;
aaa :integer;
bbb,weekint:integer;

begin

Form1.Button1Click(self);

with dm do
begin
IF Querytemp.Active THEN
IF Querytemp.RecordCount > 0 THEN
BEGIN
//confirm:=messagedlg('您要导出考勤月报表记录吗?',mtwarning,[mbyes,mbno,mbcancel],0);
//if confirm=mryes then
begin
if SaveDialog1.Execute then
begin
aaa :=LastDelimiter('/',''''+SaveDialog1.FileName+'''');
bbb :=length(''''+SaveDialog1.FileName+'''');
PathName :=copy(''''+SaveDialog1.FileName+'''', 2,aaa-1);
filename := copy(''''+SaveDialog1.FileName+'''',aaa+1,bbb-aaa-1);
end;
//filename := 'ttt';
if filename <> '' then
BEGIN
PANEL3.Visible := TRUE;
PANEL3.Update;

///########################################################################################
///########################################################################################
filename_xls := concat(extractfilepath(application.exename), 'M0001', '.xls');


try

///////////////////////////////////////////////
Excel := CreateOleObject( 'Excel.Application' );
///////////////////////////////////////////////



except
end;

try
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
//showmessage('ok');
except
Application.Messagebox('Excel 没有安装!', 'Hello', MB_ICONERROR + mb_Ok);
Abort;
end;

try

//ExcelApplication1.Workbooks.Add(EmptyParam, 0);
//ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
//ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
//filename := '';
//showmessage('1');
//

ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks.Open(filename_xls,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0));
ExcelWorkSheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);

//初始化表头
ExcelWorksheet1.Cells.item[2, 1] := v_company+COPY(YYYYMM.Text,1,4)+'年'+COPY(YYYYMM.Text,6,2)+' 月考勤月报表';
ExcelWorksheet1.Cells.item[1, 1] := DATETOSTR(SDATE.Date)+'到'+DATETOSTR(EDATE.Date)+'员工考勤结果报表';
//写入数据到EXCEL
Excel.Columns['B:B'].NumberFormatLocal:='@';
i:=5;
//第一列写序列号
//ExcelWorksheet1.Cells.item[i, 1] := '序号';
//第二列写工号
//ExcelWorksheet1.Cells.item[i, 2] := '工号';
//第三列写名字
//ExcelWorksheet1.Cells.item[i, 3] := '姓名';
j:=5;
while j < DBGrid1.FieldCount+1 do
begin
//ExcelWorksheet1.Cells.item[i, 1] := ExcelWorksheet1.Rows.;
ExcelWorksheet1.Cells.item[i, j-1] :=DBGrid1.Fields[j-1].DisplayLabel;
//标示星期几
weekint := 40;
try
weekint := strtoint(DBGrid1.Fields[j-1].DisplayLabel);
except
weekint := 40;
end;

//if DBGrid1.Fields[j-1].DisplayLabel in ('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31') then
if weekint < 40 then
begin
if v_dd = '01' then
begin
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 1 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '日';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 2 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '一';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 3 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '二';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 4 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '三';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 5 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '四';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 6 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '五';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 7 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '六';

end
else
if strtoint(DBGrid1.Fields[j-1].DisplayLabel) > strtoint(v_dd) then
begin
if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 1 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '日';

if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 2 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '一';

if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 3 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '二';

if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 4 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '三';

if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 5 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '四';

if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 6 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '五';

if dayofweek(strtodate(formatdatetime('YYYY-MM',sdate.DateTime)+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 7 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '六';
end
else
begin
if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 1 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '日';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 2 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '一';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 3 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '二';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 4 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '三';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 5 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '四';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 6 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '五';

if dayofweek(strtodate(yyyymm.Text+'-'+DBGrid1.Fields[j-1].DisplayLabel)) = 7 then
ExcelWorksheet1.Cells.item[i+1, j-1] := '六';


end;

end;
inc(j)
end;

ExcelWorksheet1.Cells.item[3, 1] := '统计部门:'+TreeView1.Selected.Text;
//写入数据到EXCEL
Querytemp.First;
i := 7;
while not Querytemp.Eof do
begin
//第一列写序列号
ExcelWorksheet1.Cells.item[i, 1] := inttostr(i-6);
//第二列写工号
ExcelWorksheet1.Cells.item[i, 2] := Querytemp.fieldbyname('smt_personnelid').Asstring;
//第三列写名字
ExcelWorksheet1.Cells.item[i, 3] := Querytemp.fieldbyname('smt_name').Asstring;
j:=5;
while j < DBGrid1.FieldCount+1 do
begin
//ExcelWorksheet1.Cells.item[i, 1] := ExcelWorksheet1.Rows.;
ExcelWorksheet1.Cells.item[i, j-1] :=Querytemp.fieldbyname(field_list[j-1]).Asstring;

inc(j)
end;
Querytemp.Next;
inc(i);
if i < Querytemp.RecordCount+6 then
Excel.ActiveSheet.Rows.Insert;
end;

Excel.Visible:=true;
Excel.Caption:='考勤月统计报表';

//打印预览
//ExcelWorksheet1.PrintPreview;
ExcelWorksheet1.SaveAs(SaveDialog1.FileName);
//导出


finally

{
Excel.ActiveWorkBook.Saved := True;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
Excel.quit;

}

Excel.ActiveWorkBook.Saved := True;
ExcelWorkbook1.Free;
ExcelWorksheet1.Free;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
Excel.quit;

end;
///########################################################################################
///########################################################################################

END;
END;
END;

end;

end;
 
这些方法中,无论Quit,还是Free,还是Nil .... Excel进程退不出的问题,还是会发生。
我也不知如何解决啊。
 
同意楼上说的,我现在也正在为这个问题犯愁。
 
except //try Except
if dts.ControlsDisabled then dts.EnableControls ;
if not VarIsEmpty(xls) then //测试Excel是否存在
begin
xls.displayAlerts:=false; //退出时不提示保存
xls.quit; //退出Excel
xls:=unassigned;
//xls.displayAlerts:=false;让对象不可见
end;
showmessage('导出数据时出现错误,可能原因:没有正确安装Excel;系统所使用的报表文件已打开,正在使用中!');
end; //try Except
 
我也遇到此类问题啊,不间断地会报错。
 
关闭excel的时候用
ExcelApplication1.Quit;
    ExcelWorksheet1.Disconnect;
    ExcelWorkbook1.Disconnect;
    ExcelApplication1.Disconnect;

就可以了
 
这个帖子怎么又浮起来了,怎么大家都那么懒啊。查查旧贴不就知道了吗?
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
HWndCalculator: HWnd;
begin
HwndCalculator := Winprocs.FindWindow(nil, 'Microsoft Excel - score.xls');
if HwndCalculator <> 0 then
SendMessage(HwndCalculator, WM_CLOSE, 0, 0);
end;
注意:score.xls是我的文件名,你的应当跟我不一样吧。
 
有这么麻烦吗?
我以前写过的好几个需要excel的程序

finally
preview.quit;
preview:=unassigned;
end;

只要调用过这2句进程里的excel肯定就消失了,没发现过未消失的情况。
不知道楼上那些说怎么也消失不掉的人们的程序是怎么搞的
 
操作完后执行
preview.quit就可以了
 
我得问题是解决了了,我申明了几个全局变量 range,sheet ,把他们也置为unassigned就行了。
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
618
import
I
I
回复
0
查看
781
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
547
import
I
后退
顶部