B
blackeagle136
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠:
这段程序是将多个EXCEL文件合并为一个文件,操作完成后已经退出了EXCEL并释放了变量,但是任务管理器里还是有EXCEL进程,这个问题已经困扰我很多天了,请各位大侠帮忙看看问题出在哪里,多谢!
procedure TCollect.copydata(Sender: TObject);
var
fnames,fnamed,INIFileName,fnr: string;
mwhd,fnc,IC,ffn,iv: integer;
begin
iv:=1;
fnamed:='汇总模板';
ExApp := CreateOleObject( 'Excel.Application' );
ExApp.Visible:=False;
ExApp.WorkBooks.Open( ExtractFilePath(Application.Exename)+fnamed+'.xls');
INIFileName:=ExtractFilePath(paramstr(0))+'/'+'config.ini';
MyIniFile:=TIniFile.Create(INIFileName);
IC:=StrToInt(MyIniFile.ReadString('文件名','FNC','err'));
ProgressBar.Max :=IC;
for fnc:=1 to IC do
begin
ProgressBar.StepBy(1);
fnames:=MyIniFile.ReadString('文件名','FN'+IntToStr(fnc),'err');
doing.Caption:=fnames;
Application.ProcessMessages;
ExApp.WorkBooks.Open(Pathshow.Text+'/'+fnames+'.xls',EmptyParam,True);
sheetd:=ExApp.WorkBooks[1].WorkSheets[1];
sheets:=ExApp.WorkBooks[2].WorkSheets[1];
if ExApp.WorkBooks[1].readon_ly=true then
begin
showmessage('目标文件只读,可能已经打开,请关闭文件后再试一次。');
ExApp.DisplayAlerts:=false;
ExApp.quit;
ExApp:=Unassigned;
sheets:=Unassigned;
sheetd:=Unassigned;
exit;
end;
sheets.Activate;
mwhd:=sheetd.UsedRange.rows.count;
if ExApp.CountA(sheetd.Rows[mwhd]) = 0 then
begin
repeat
mwhd:=mwhd-1;
until
ExApp.CountA(sheetd.Rows[mwhd]) <> 0;
end;
if FillName.Checked then
for ffn:=iv to mwhd-1 do
sheetd.Cells[ffn+1,11].value:=fnr;
iv:=mwhd+1;
fnr:=fnames;
if ExApp.CountA(sheetd.Rows[mwhd+1]) = 0 then
begin
sheets.UsedRange.copy;
sheetd.Activate;
sheetd.Range['A'+IntToStr(mwhd+1)].Select;
sheetd.Paste;
if (FillName.Checked) and (fnc=IC) then
begin
mwhd:=sheetd.UsedRange.rows.count;
repeat
mwhd:=mwhd-1;
until
ExApp.CountA(sheetd.Rows[mwhd]) <> 0;
for ffn:=iv to mwhd-1 do
sheetd.Cells[ffn+1,11].value:=fnames;
end;
end
else
begin
showmessage('末尾行计算错误,为防止数据被覆盖,将终止操作,请检查。'+inttostr(mwhd));
ExApp.DisplayAlerts:=false;
ExApp.quit;
ExApp:=Unassigned;
sheets:=Unassigned;
sheetd:=Unassigned;
exit;
end;
//关闭EXCEL源文件
//ExApp.WorkBooks[1].save;
ExApp.DisplayAlerts:=False;
ExApp.WorkBooks[2].close;
end;
ExApp.DisplayAlerts:=False;
sheetd.Cells[5,5].Select;
ExApp.WorkBooks[1].SaveAs(Pathshow.Text+'/'+'汇总.xls');
ExApp.WorkBooks[1].close;
ExApp.quit;
ExApp:=Unassigned;
sheets:=Unassigned;
sheetd:=Unassigned;
doing.Caption:='汇总完成。';
Application.Terminate;
end;
这段程序是将多个EXCEL文件合并为一个文件,操作完成后已经退出了EXCEL并释放了变量,但是任务管理器里还是有EXCEL进程,这个问题已经困扰我很多天了,请各位大侠帮忙看看问题出在哪里,多谢!
procedure TCollect.copydata(Sender: TObject);
var
fnames,fnamed,INIFileName,fnr: string;
mwhd,fnc,IC,ffn,iv: integer;
begin
iv:=1;
fnamed:='汇总模板';
ExApp := CreateOleObject( 'Excel.Application' );
ExApp.Visible:=False;
ExApp.WorkBooks.Open( ExtractFilePath(Application.Exename)+fnamed+'.xls');
INIFileName:=ExtractFilePath(paramstr(0))+'/'+'config.ini';
MyIniFile:=TIniFile.Create(INIFileName);
IC:=StrToInt(MyIniFile.ReadString('文件名','FNC','err'));
ProgressBar.Max :=IC;
for fnc:=1 to IC do
begin
ProgressBar.StepBy(1);
fnames:=MyIniFile.ReadString('文件名','FN'+IntToStr(fnc),'err');
doing.Caption:=fnames;
Application.ProcessMessages;
ExApp.WorkBooks.Open(Pathshow.Text+'/'+fnames+'.xls',EmptyParam,True);
sheetd:=ExApp.WorkBooks[1].WorkSheets[1];
sheets:=ExApp.WorkBooks[2].WorkSheets[1];
if ExApp.WorkBooks[1].readon_ly=true then
begin
showmessage('目标文件只读,可能已经打开,请关闭文件后再试一次。');
ExApp.DisplayAlerts:=false;
ExApp.quit;
ExApp:=Unassigned;
sheets:=Unassigned;
sheetd:=Unassigned;
exit;
end;
sheets.Activate;
mwhd:=sheetd.UsedRange.rows.count;
if ExApp.CountA(sheetd.Rows[mwhd]) = 0 then
begin
repeat
mwhd:=mwhd-1;
until
ExApp.CountA(sheetd.Rows[mwhd]) <> 0;
end;
if FillName.Checked then
for ffn:=iv to mwhd-1 do
sheetd.Cells[ffn+1,11].value:=fnr;
iv:=mwhd+1;
fnr:=fnames;
if ExApp.CountA(sheetd.Rows[mwhd+1]) = 0 then
begin
sheets.UsedRange.copy;
sheetd.Activate;
sheetd.Range['A'+IntToStr(mwhd+1)].Select;
sheetd.Paste;
if (FillName.Checked) and (fnc=IC) then
begin
mwhd:=sheetd.UsedRange.rows.count;
repeat
mwhd:=mwhd-1;
until
ExApp.CountA(sheetd.Rows[mwhd]) <> 0;
for ffn:=iv to mwhd-1 do
sheetd.Cells[ffn+1,11].value:=fnames;
end;
end
else
begin
showmessage('末尾行计算错误,为防止数据被覆盖,将终止操作,请检查。'+inttostr(mwhd));
ExApp.DisplayAlerts:=false;
ExApp.quit;
ExApp:=Unassigned;
sheets:=Unassigned;
sheetd:=Unassigned;
exit;
end;
//关闭EXCEL源文件
//ExApp.WorkBooks[1].save;
ExApp.DisplayAlerts:=False;
ExApp.WorkBooks[2].close;
end;
ExApp.DisplayAlerts:=False;
sheetd.Cells[5,5].Select;
ExApp.WorkBooks[1].SaveAs(Pathshow.Text+'/'+'汇总.xls');
ExApp.WorkBooks[1].close;
ExApp.quit;
ExApp:=Unassigned;
sheets:=Unassigned;
sheetd:=Unassigned;
doing.Caption:='汇总完成。';
Application.Terminate;
end;