Excel 2007中挂起时通过.NET关闭 [英] Excel 2007 Hangs When Closing via .NET

查看:228
本文介绍了Excel 2007中挂起时通过.NET关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要打开和关闭Excel US preadsheet一个Visual Basic .NET程序。打开和阅读的S preadsheet做工精细,但试图关闭Excel 2007中的应用程序会导致挂起。这似乎关闭,但如果你看看在任务管理器的应用程序仍在运行。我使用关闭它的code是

I have a Visual Basic .NET program which needs to open and close an Excel spreadsheet. Opening and reading the spreadsheet work fine, but trying to close the Excel 2007 application causes it to hang. It seems to close, but if you look in the task manager the application is still running. The code that I'm using to close it is

wbkData.Close(saveChanges:=False)
appExcel.Quit()
wbkData = Nothing
appExcel = Nothing

我怎样才能得到Excel中正常关闭?

How can I get Excel to close properly?

推荐答案

在回答你的问题已经被这里介绍我认为: <一href="http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c">How妥善清理在C Excel的互操作对象

The answer to your question has been covered here i think: How to properly clean up excel interop objects in c

我无法从你的code样品看,但基本上,始终分配您的Excel对象,以局部变量,永远不会'两个点下,像这样的:

i cant see from your code sample, but basically, always assign your excel objects to local variables, never going 'two dots down', like this:

//FAIL

Workbook wkBook = xlApp.Workbooks.Open(@"C:\mybook.xls");

而不是裁判的每个单独的OBJ:

instead ref each obj individually:

//WIN

Worksheets sheets = xlApp.Worksheets;
Worksheet sheet = sheets.Open(@"C:\mybook.xls");
...
Marshal.ReleaseComObject(sheets);
Marshal.ReleaseComObject(sheet);

.NET创建一个包装的COM对象是看不见的,你并没有公布,直到GC编织它的魔力。

.NET creates a wrapper for the COM object that is invisible to you and is not released until the GC weaves its magic.

直到我发现了这一点,我在每次运行哈克code以下的ASP.NET应用程序中,我创建了一个检查EXCEL.EXE进程的年龄和杀死任何超过一分钟的一个新的工作簿老:

Until I discovered this, I was running the hacky code below in an ASP.NET application each time i created a new workbook that that checks the age of the excel.exe process and kills any that are over a minute old:

//force kill any excel processes over one minute old.
try
{
    Process[] procs = Process.GetProcessesByName("EXCEL");
    foreach (Process p in procs)
    {
    	if (p.StartTime.AddMinutes(1) < DateTime.Now)
    	{
    		p.Kill(); 
    	}  
    }  
}
catch (Exception)
{}

这篇关于Excel 2007中挂起时通过.NET关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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