百分之百的老调重谈!!excel的释放(50分)

  • 主题发起人 主题发起人 苦海无边
  • 开始时间 开始时间
俺通常在释放完后 再执行一次强行关闭 EXCEL的过程
 
procedure TFsettle.FormCreate(Sender: TObject);
begin
querylist := tquery.create(self);
querylist.DatabaseName := 'book';
querylist.SQL.Add('select departcurrency.dept,department_kc_change.dept from departcurrency, department_kc_change');
querylist.SQL.Add('where (departcurrency."date" =:_date) and (department_kc_change."date" =:_date)');
querylist.ParamByName('_date').AsDate := date;
querylist.Open;
button2.Enabled := (querylist.Fields[0].AsString = '');
button3.Enabled := not button2.Enabled;
button5.Enabled := button2.Enabled;
button6.Enabled := button2.Enabled;
querylist.Close;
querylist.SQL.Clear;


label1.Caption := formatdatetime('yyyy"Äê"mm"ÔÂ"dd"ÈÕ"½áÕË', date);

sellgrid.cells[0, 0] := 'Àà±ð';
sellgrid.cells[1, 0] := 'ÂëÑó';
sellgrid.cells[2, 0] := 'ʵÑó';
sellgrid.cells[3, 0] := '³É±¾';
sellgrid.cells[4, 0] := 'ÀûÈó';
sellgrid.cells[5, 0] := '±¸×¢';
sellgrid.cells[6, 0] := '״̬';

sellgrid.ColWidths[7] := -1;
sellgrid.ColWidths[8] := -1;
sellgrid.ColWidths[9] := -1;

inoutgrid.Cells[0, 0] := 'Àà±ð';
inoutgrid.Cells[1, 0] := 'µ¥ºÅ';
inoutgrid.Cells[2, 0] := 'µ¥Î»';
inoutgrid.Cells[3, 0] := '²Ù×÷Ô±';
inoutgrid.Cells[4, 0] := 'ÂëÑó';
inoutgrid.Cells[5, 0] := 'ʵÑó';
inoutgrid.Cells[6, 0] := 'ÊýÁ¿';
inoutgrid.Cells[7, 0] := 'ÕÛ¿Û';

inoutgrid.ColWidths[8] := -1;

formmain.out_excel := CreateOleObject('Excel.Application');//生成excel对象
end;

procedure TFsettle.Button7Click(Sender: TObject);
var
queryout: tquery;
temp_i, temp_j: integer;
begin
formmain.out_workbooks := formmain.out_excel.workbooks.add(-4167);//+各工作簿
queryout := tquery.Create(self);
queryout.DatabaseName := 'book';
queryout.SQL.Add('SELECT Sl, Bookname, Xlh, Dj, sell."date", Num, Zk, Ghdw');
queryout.SQL.Add('FROM Sell');
queryout.SQL.Add('WHERE (Bookname <> "ÆÚ¿¯")');
queryout.SQL.Add(' AND (Bookname <> "ÒôÏñ")');
queryout.SQL.Add(' AND (Bookname <> "Íæ¾ß")');
queryout.SQL.Add(' AND (Bookname <> "ÎÄ»¯ÓÃÆ·")');
queryout.SQL.Add(' AND (sell."date" =:_date)');
queryout.parambyname('_date').asdate := date;
queryout.Open;
if (queryout.Fields[1].AsString <> '') then
begin
with formmain do
begin
out_worksheets := out_excel.worksheets.add;
out_worksheets.name := 'UPsell';
out_worksheets.Cells.Select;
out_excel.Selection.NumberFormatLocal := '@';
try
gauge1.MaxValue := queryout.RecordCount;
gauge1.ForeColor := clRed;
for temp_i := 1 to queryout.RecordCount do
begin
out_worksheets.cells[temp_i, 1] := queryout.Fields[0].AsString;
out_worksheets.cells[temp_i, 2] := queryout.Fields[1].AsString;
out_worksheets.cells[temp_i, 3] := queryout.Fields[2].AsString;
out_worksheets.cells[temp_i, 4] := queryout.Fields[3].AsString;
out_worksheets.cells[temp_i, 5] := dbbook.TablecompanyDeptId.Value;
out_worksheets.cells[temp_i, 6] := queryout.Fields[4].AsString;
out_worksheets.cells[temp_i, 7] := queryout.Fields[5].AsString;
out_worksheets.cells[temp_i, 8] := queryout.Fields[6].AsString;
out_worksheets.cells[temp_i, 9] := queryout.Fields[7].AsString;
out_worksheets.cells[temp_i, 10] := 'ÏúÊÛ¿Í»§';
queryout.Next;
gauge1.Progress := temp_i;
end;
except
end;
end;
end;
//====================================================
queryout.sql.Clear;
queryout.SQL.Add('SELECT Buybook.Sl, Buybook.Bookname, Buybook.xlh, Buybook.Dj, buybook.rkrq, buybook.rkd, buybook.rkzk, buybook.ghdw');
queryout.SQL.Add('From buybook');
queryout.SQL.Add('inner join buybookinfo');
queryout.SQL.Add(' on (buybook.rkd = buybookinfo.rkd)');
queryout.SQL.Add('where buybook.rkrq =:_date');
queryout.SQL.Add(' and (buybookinfo.posted = true)');
queryout.ParamByName('_date').asdate := date;
queryout.Close;
queryout.Open;
if (queryout.Fields[1].AsString <> '') then
begin
with formmain do
begin
out_worksheets := out_excel.worksheets.add;
out_worksheets.name := 'UPbuybook';
out_worksheets.Cells.Select;
out_excel.Selection.NumberFormatLocal := '@';
try
gauge1.MaxValue := queryout.RecordCount;
gauge1.ForeColor := clYellow;
for temp_i := 1 to queryout.RecordCount do
begin
out_worksheets.cells[temp_i, 1] := queryout.Fields[0].AsString;
out_worksheets.cells[temp_i, 2] := queryout.Fields[1].AsString;
out_worksheets.cells[temp_i, 3] := queryout.Fields[2].AsString;
out_worksheets.cells[temp_i, 4] := queryout.Fields[3].AsString;
out_worksheets.cells[temp_i, 5] := dbbook.TablecompanyDeptId.Value;
out_worksheets.cells[temp_i, 6] := queryout.Fields[4].AsString;
out_worksheets.cells[temp_i, 7] := queryout.Fields[5].AsString;
out_worksheets.cells[temp_i, 8] := queryout.Fields[6].AsString;
out_worksheets.cells[temp_i, 9] := queryout.Fields[7].AsString;
out_worksheets.cells[temp_i, 10] := queryout.Fields[7].Asstring;
queryout.Next;
gauge1.Progress := temp_i;
end;
except
end;
end;
end;
//====================================================
queryout.SQL.Clear;
queryout.SQL.Add('SELECT Move.Sl, Move.NAME, Move.Xlh, Move.Dj, Move."Date", Move.Ykd, Move.Bag_num, Move.Zk, Move.Ghdw, Move.Place');
queryout.SQL.Add('FROM Move');
queryout.SQL.Add('INNER JOIN Move_bag');
queryout.SQL.Add(' ON (Move.Bag_num = Move_bag.Bag_num)');
queryout.SQL.Add('WHERE (Move."Date" =:_date)');
queryout.SQL.Add(' AND (Move_bag.Posted = TRUE)');
queryout.parambyname('_date').asdate := date;
queryout.Close;
queryout.Open;
if (queryout.Fields[1].AsString <> '') then
begin
with formmain do
begin
out_worksheets := out_excel.worksheets.add;
out_worksheets.name := 'UPmove';
out_worksheets.Cells.Select;
out_excel.Selection.NumberFormatLocal := '@';
try
gauge1.MaxValue := queryout.RecordCount;
gauge1.ForeColor := clYellow;
for temp_i := 1 to queryout.RecordCount do
begin
out_worksheets.cells[temp_i, 1] := queryout.Fields[0].AsString;
out_worksheets.cells[temp_i, 2] := queryout.Fields[1].AsString;
out_worksheets.cells[temp_i, 3] := queryout.Fields[2].AsString;
out_worksheets.cells[temp_i, 4] := queryout.Fields[3].AsString;
out_worksheets.cells[temp_i, 5] := dbbook.TablecompanyDeptId.Value;
out_worksheets.cells[temp_i, 6] := queryout.Fields[4].AsString;
out_worksheets.cells[temp_i, 7] := queryout.Fields[5].AsString + '_' + queryout.Fields[6].AsString;
out_worksheets.cells[temp_i, 8] := queryout.Fields[7].AsString;
out_worksheets.cells[temp_i, 9] := queryout.Fields[8].AsString;
out_worksheets.cells[temp_i, 10] := queryout.Fields[9].Asstring;
queryout.Next;
gauge1.Progress := temp_i;
end;
except
end;
end;
end;

//======================================================
queryout.SQL.Clear;
queryout.SQL.Add('SELECT Bigsell.Sl , Bigsell.Bookname, Bigsell.Xlh, Bigsell.Price, bigsell."date", Bigsell.number, Bigsell.pfzk, bigsell.ghdw,Bigsell.pfdw');
queryout.SQL.Add('FROM Bigsell');
queryout.SQL.Add(' INNER JOIN Bigsellinfo');
queryout.SQL.Add(' ON (Bigsell.Number = Bigsellinfo.Number)');
queryout.SQL.Add('WHERE (Bigsell."Date" =:_date)');
queryout.SQL.Add(' AND (Bigsellinfo.Posted = TRUE)');
queryout.ParamByName('_date').AsDate := date;
queryout.Close;
queryout.Open;
if (queryout.Fields[1].AsString <> '') then
begin
with formmain do
begin
out_worksheets := out_excel.worksheets.add;
out_worksheets.name := 'UPbigsell';
out_worksheets.Cells.Select;
out_excel.Selection.NumberFormatLocal := '@';
try
gauge1.MaxValue := queryout.RecordCount;
gauge1.ForeColor := clGreen;
for temp_i := 1 to queryout.RecordCount do
begin
out_worksheets.cells[temp_i, 1] := queryout.Fields[0].AsString;
out_worksheets.cells[temp_i, 2] := queryout.Fields[1].AsString;
out_worksheets.cells[temp_i, 3] := queryout.Fields[2].AsString;
out_worksheets.cells[temp_i, 4] := queryout.Fields[3].AsString;
out_worksheets.cells[temp_i, 5] := dbbook.TablecompanyDeptId.Value;
out_worksheets.cells[temp_i, 6] := queryout.Fields[4].AsString;
out_worksheets.cells[temp_i, 7] := queryout.Fields[5].AsString;
out_worksheets.cells[temp_i, 8] := queryout.Fields[6].AsString;
out_worksheets.cells[temp_i, 9] := queryout.Fields[7].AsString;
out_worksheets.cells[temp_i, 10] := queryout.Fields[8].AsString;
queryout.Next;
gauge1.Progress := temp_i;
end;
except
end;
end;
end;

//=========================================================================
queryout.SQL.Clear;
queryout.SQL.Add('SELECT ID, Name, Sex, Age, Birth, Address, Phone, Start_date, Last_time_use, Total_TMoney, Total_FMoney, Sum_number, Remark, Shopping_num, Day_changeF, Day_changeT, Deptid, Uactive');
queryout.SQL.Add('FROM "User.db"');
queryout.SQL.Add('WHERE (Day_changeF > 0) OR (Uactive = False)');
queryout.Close;
queryout.Open;

if not queryout.IsEmpty then
begin
formmain.out_worksheets := formmain.out_excel.worksheets.add;
formmain.out_worksheets.name := 'UPdata';
formmain.out_worksheets.Cells.Select;
formmain.out_excel.Selection.NumberFormatLocal := '@';
try
gauge1.MaxValue := queryout.RecordCount;
gauge1.ForeColor := clWhite;
for temp_i := 1 to queryout.RecordCount do
begin
for temp_j := 1 to queryout.FieldCount do
if (queryout.Fields[temp_j - 1].asstring <> '') then
formmain.out_worksheets.cells[temp_i, temp_j] := queryout.Fields[temp_j - 1].asstring
else
formmain.out_worksheets.cells[temp_i, temp_j] := 'δÌîд';

queryout.Next;
gauge1.Progress := temp_i;
end;
except
end;
end;

formmain.out_excel.DisplayAlerts := False;
formmain.out_excel.worksheets['Sheet1'].Delete;
formmain.out_excel.DisplayAlerts := True;
formmain.out_workbooks.saveas(Filename := dbbook.Tablecompanydept.Value + '½áÕË' + datetostr(date));
formmain.out_workbooks.close;//关闭。。。。。。。。。。。
formmain.out_excel.quit;///退出
queryout.Free;

showmessage(dbbook.Tablecompanydept.Value + '½áÕË' + datetostr(date) + ' ÒѾ­µ¼³öµ½ÎÒµÄÎĵµÄ¿Â¼');
gauge1.Progress := 0;
end;

procedure TFsettle.FormClose(Sender: TObject; var Action: TCloseAction);
begin
formmain.out_excel :=Unassigned;///释放
querylist.Free;
Action := cafree;
end;
另外,这个问题,都是在98下面出的,我自己调试的时候,都能够杀掉,我实在想不到这样有什么异常。
 
to 苦海无边
哇你的程序好长,对不起我没有98,也不好帮你测试,但是你的程序我看了2遍,一下是我的几个建议:
1/try except 会屏蔽系统所有的异常,所以如果你想知道你的异常是怎么发生的,请在except 处保存异常信息.
2/你的 try except 保护并不完整有部分代码没有保护起来,而这些代码不一定就不会发生异常
3/你的 out_worksheets formmain.out_workbooks 按我的猜测应该也是 2 个接口但是你并没有明显的释放 unassigned,可能造成内存泄漏.
4/在 com 之间传送数据尽量不要使用 out_worksheets.cells[temp_i, 5] := dbbook.TablecompanyDeptId.Value; 因为 Value 的兼容性很难保证,你最好将 Value 先转换成 String.
5/out_worksheets := out_excel.worksheets.add; formmain.out_workbooks := formmain.out_excel.workbooks.add(-4167); formmain.out_worksheets := formmain.out_excel.worksheets.add; 等等操作根本没有恢复代码,也就是说出错的处理,如果第一次产生了异常没处理,那么用户有重新点击一次按钮,根本无法预计程序的走向....

以上5个建议一定能帮助找到你的真正出错的位置,也可以让你的程序更强壮.


 
delp请到这里
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2050636
 
procedure TForm1.FormDestroy(Sender: TObject);
var
excel:HWND;
begin
excel:=Findwindow('XLMAIN',nil);//根据标题查找句柄
if excel>0 then
begin
sendMessage(excel,WM_CLOSE,0,0);//发送关闭消息
end;
end;


在执行以上代码.EXCEL就释放了
 

Similar threads

D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部