Excel进程没有关闭 [英] Excel process not closing
问题描述
编辑:我尝试了提到的事情,但仍然不会关闭。这是我更新的代码
编辑:尝试整个Process.Kill(),它的工作原理,但似乎是一个黑客的东西,应该只是工作。
public class CompareHelper
{
//定义变量
Excel.Application excelApp = null;
Excel.Workbooks wkbks = null;
Excel.Workbook wkbk = null;
Excel.Worksheet wksht = null;
字典< String,int> map = new Dictionary< String,int>();
//比较列
public void GetCounts(string startrow,string endrow,string columnsin,System.Windows.Forms.TextBox results,string excelFile)
{
result.Text =;
try
{
//创建一个Microsoft Excel的实例,使其不可见
excelApp = new Excel.Application();
excelApp.Visible = false;
//打开一个工作簿并获取活动的工作表
wkbks = excelApp.Workbooks;
wkbk = wkbks.Open(excelFile,Type.Missing,true);
wksht = wkbk.ActiveSheet;
...
}
catch
{
throw;
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
if(wksht!= null)
{
//wksht.Delete();
Marshal.FinalReleaseComObject(wksht);
wksht = null;
}
if(wkbks!= null)
{
//wkbks.Close();
Marshal.FinalReleaseComObject(wkbks);
wkbks = null;
}
if(wkbk!= null)
{
excelApp.DisplayAlerts = false;
wkbk.Close(false,Type.Missing,Type.Missing);
Marshal.FinalReleaseComObject(wkbk);
wkbk = null;
}
如果(excelApp!= null)
{
excelApp.Quit();
Marshal.FinalReleaseComObject(excelApp);
excelApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
/ *
Process [] processes = Process.GetProcessesByName(EXCEL);
foreach(进程p在进程中)
{
p.Kill();
}
* /
}
}
}
这是一个关于办公应用程序在.NET应用程序与其断开连接后保持打开状态的有趣的知识库。
Office应用程序在从Visual Studio .NET客户端自动化后不会退出
代码示例都在链接(vb.net对不起)。基本上它显示了如何正确设置和拆除办公室应用程序,以便在完成后关闭它。
System.Runtime.InteropServices.Marshal.FinalReleaseComObject
是魔术发生的地方。
编辑:您需要为您创建的每个excel对象调用FinalReleaseComObject。
if(excelWorkSheet1!= null)
{
Marshal.FinalReleaseComObject(excelWorkSheet1);
excelWorkSheet1 = null;
}
if(excelWorkbook!= null)
{
Marshal.FinalReleaseComObject(excelWorkbook);
excelWorkbook = null;
}
if(excelApp!= null)
{
Marshal.FinalReleaseComObject(excelApp);
excelApp = null;
}
I've got this C# program that never closes the Excel process. Basically it finds the number of instances a string appears in a range in Excel. I've tried all kinds of things, but it's not working. There is a Form that is calling this method, but that shouldn't change why the process isn't closing. I've looks at suggestions by Hans Passant, but none are working.
EDIT: I tried the things mentioned and it still won't close. Here's my updated code. EDIT: Tried the whole Process.Kill() and it works, but it seems like a bit of a hack for something that should just work.
public class CompareHelper
{
// Define Variables
Excel.Application excelApp = null;
Excel.Workbooks wkbks = null;
Excel.Workbook wkbk = null;
Excel.Worksheet wksht = null;
Dictionary<String, int> map = new Dictionary<String, int>();
// Compare columns
public void GetCounts(string startrow, string endrow, string columnsin, System.Windows.Forms.TextBox results, string excelFile)
{
results.Text = "";
try
{
// Create an instance of Microsoft Excel and make it invisible
excelApp = new Excel.Application();
excelApp.Visible = false;
// open a Workbook and get the active Worksheet
wkbks = excelApp.Workbooks;
wkbk = wkbks.Open(excelFile, Type.Missing, true);
wksht = wkbk.ActiveSheet;
...
}
catch
{
throw;
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (wksht != null)
{
//wksht.Delete();
Marshal.FinalReleaseComObject(wksht);
wksht = null;
}
if (wkbks != null)
{
//wkbks.Close();
Marshal.FinalReleaseComObject(wkbks);
wkbks = null;
}
if (wkbk != null)
{
excelApp.DisplayAlerts = false;
wkbk.Close(false, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(wkbk);
wkbk = null;
}
if (excelApp != null)
{
excelApp.Quit();
Marshal.FinalReleaseComObject(excelApp);
excelApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
/*
Process[] processes = Process.GetProcessesByName("EXCEL");
foreach (Process p in processes)
{
p.Kill();
}
*/
}
}
}
Here is an interesting knowledge base on the subject of office apps staying open after a .NET app disconnects from them.
Office application does not quit after automation from Visual Studio .NET client
The code examples are all in the link (vb.net sorry). Basically it shows you how to correctly setup and tear down the office app so that it closes when you're finished with it.
System.Runtime.InteropServices.Marshal.FinalReleaseComObject
is where the magic happens.
EDIT: You need to call the FinalReleaseComObject for each excel object that you've created.
if (excelWorkSheet1 != null)
{
Marshal.FinalReleaseComObject(excelWorkSheet1);
excelWorkSheet1 = null;
}
if (excelWorkbook != null)
{
Marshal.FinalReleaseComObject(excelWorkbook);
excelWorkbook = null;
}
if (excelApp != null)
{
Marshal.FinalReleaseComObject(excelApp);
excelApp = null;
}
这篇关于Excel进程没有关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!