为什么这个代码导致Excel无法正常关闭? [英] Why does this code cause Excel to not close properly?

查看:162
本文介绍了为什么这个代码导致Excel无法正常关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这行代码导致Excel不退出?



  Excel.Range范围=(Excel.Range)WS .Cells [1,1]; 

如果是因为铸造的,那么就不该代码导致同样的问题?

  Excel.Worksheet WS =(Excel.Worksheet)wb.ActiveSheet; 



我试过。但这个工程。 Excel将关闭。



如果我用这个代码。 。Excel中关闭

  Excel.Range范围= ws.get_Range(A1,A1); 



所以,有什么区别呢?是的,我知道有像如何正确关闭Excel线程万元。但由于这是一个问题,不是一个答案,我决定问一个新的要求,而不是在其他人的问题。



下面是我的代码。不过,当然还有隔着其他代码。我只是注释掉一切,慢慢尝试哪个线导致Excel不能关闭。我认识到,即使不使用垃圾回收器,Excel中仍然关闭。我不想用大锤关闭Excel。



感谢。

  Excel.Application objExcel =新Excel.Application(); 
Excel.Workbooks WBS = objExcel.Workbooks;
Excel.Workbook WB = wbs.Open(saveFileDialog1.FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,类型.Missing,Type.Missing,Type.Missing,Type.Missing);
Excel.Worksheet WS =(Excel.Worksheet)wb.ActiveSheet;
//Excel.Range范围=(Excel.Range)ws.Cells [1,1];
Excel.Range范围= ws.get_Range(A1,A1);

FinalReleaseAnyComObject(范围);
FinalReleaseAnyComObject(WS);
wb.Close(Type.Missing,Type.Missing,Type.Missing);
FinalReleaseAnyComObject(WB);
FinalReleaseAnyComObject(WBS);
objExcel.Quit();
FinalReleaseAnyComObject(objExcel);

目前我已经试过直到objExcel,WBS,WB和WS。这4个对象不会引起问题。

 私有静态无效FinalReleaseAnyComObject(对象o)
{
Marshal.FinalReleaseComObject(O);
O = NULL;
}



我知道你不能重用的变量也是如此。

  Excel.Range范围= ws.get_Range(A1,G1); 
范围= ws.get_Range(A1,A1);

这将导致Excel无法正常关闭了。相反,使用



  Excel.Range范围= ws.get_Range(A1,G1); 
FinalReleaseAnyComObject(范围);
范围= ws.get_Range(A1,A1);


解决方案

有一个隐藏的范围接口指针,你不能看到,单元格属性返回。然后应用索引表达式来了,提领该范围的默认项属性。为了得到另一个范围。



这就是为什么它是这样一个坏,坏主意,试图管理的COM接口指针自己。相信垃圾收集器的总是的得到它的权利。所以GC.Collect()和GC.WaitForPendingFinalizers()如果你真的想使它退出需求。


Why does this line of code cause Excel not to exit?

Excel.Range range = (Excel.Range)ws.Cells[1,1];

If it's because of the casting, then wouldn't this code cause the same problem?

Excel.Worksheet ws = (Excel.Worksheet)wb.ActiveSheet;

I've tried. But this works. Excel will close.

If I use this code. Excel closes.

Excel.Range range = ws.get_Range("A1","A1");

So what's the difference? Yes I know there's like a million of "How to close Excel properly" threads. But since this is a question and not an answer, I decided to ask a new one instead of asking in other people's question.

Here's my code. But of course there's another codes in between. I'm just commenting out everything and slowly trying out which lines causes Excel to not close. I realise that even without using Garbage collector, Excel still closes. I do not want to use a sledgehammer to close Excel.

Thanks.

Excel.Application objExcel = new Excel.Application();
Excel.Workbooks wbs = objExcel.Workbooks;
Excel.Workbook wb = wbs.Open(saveFileDialog1.FileName, Type.Missing,  Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Worksheet ws = (Excel.Worksheet)wb.ActiveSheet;
//Excel.Range range = (Excel.Range)ws.Cells[1,1];
Excel.Range range = ws.get_Range("A1","A1");

FinalReleaseAnyComObject(range);
FinalReleaseAnyComObject(ws);
wb.Close(Type.Missing, Type.Missing, Type.Missing);
FinalReleaseAnyComObject(wb);
FinalReleaseAnyComObject(wbs);
objExcel.Quit();
FinalReleaseAnyComObject(objExcel);

Currently I've tried until objExcel, wbs, wb and ws. These 4 objects does not cause a problem.

private static void FinalReleaseAnyComObject(object o)
{
    Marshal.FinalReleaseComObject(o);
    o = null;
}

I realise that you cannot reuse the variable as well.

Excel.Range range = ws.get_Range("A1","G1");
range = ws.get_Range("A1", "A1");

This will cause Excel not to close properly too. Instead, use this.

Excel.Range range = ws.get_Range("A1","G1");
FinalReleaseAnyComObject(range);
range = ws.get_Range("A1", "A1");

解决方案

There's a hidden Range interface pointer that you can't see, the Cells property returns it. You then apply the indexer expression to it, dereferencing the default Item property of that Range. To get another Range.

This is why it is such a bad, bad idea to try to manage COM interface pointers yourself. Trust the garbage collector to always get it right. GC.Collect() and GC.WaitForPendingFinalizers() if you really, really want to make it quit on demand.

这篇关于为什么这个代码导致Excel无法正常关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆