互操作的Excel不关闭过程 [英] Interop Excel not closing process
问题描述
我一直在努力解决这个问题了几天,我已经reasearch和应用都是我在各种论坛中找到的建议,但我仍然无法去解决它。
I have been struggling with this problem for a couple of days, I have made reasearch and applied all the suggestions I found on various forums but I'm still unable to solve it.
我的问题是使用Excel互操作库,我作为模板Excel文件,所以我用一个新名称将其打开和保存新的位置。除了Excel进程不断被创建和关闭后的文件乳宁一切都很正常。
My problem is with excel using interop library, I have an excel file used as template, so I am opening it and saving in a new location with a new name. Everything works great except that the Excel process keeps runing after the file is created and closed.
这是我的code
protected string CreateExcel(string strProjectID, string strFileMapPath)
{
string strCurrentDir = HttpContext.Current.Server.MapPath("~/Reports/Templates/");
string strFile = "Not_Created";
Application oXL;
Workbook oWB;
oXL = new Application();
oXL.Visible = false;
Workbooks wbks = oXL.Workbooks;
//opening template file
oWB = wbks.Open(strFileMapPath);
oXL.Visible = false;
oXL.UserControl = false;
strFile = strProjectID + "_" + DateTime.Now.Ticks.ToString() + ".xlsx";
//Saving file with new name
oWB.SaveAs(strCurrentDir + strFile, XlFileFormat.xlWorkbookDefault, null, null, false, false, XlSaveAsAccessMode.xlExclusive, false, false, null, null);
oWB.Close(false, strCurrentDir + strFile, Type.Missing);
wbks.Close();
oXL.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
oWB = null;
oXL = null;
wbks = null;
GC.Collect();
return strFile;
}
正如你可以看到我将结束并释放所有对象,但应用程序不会退出。
As you can see I am closing and releasing all the objects but the application does not quit.
我在Windows Server 2008(生产)和Windows 7(开发)都在IIS7 32位测试。
I'm testing in a Windows Server 2008(production) and Windows 7(development) both in 32bits with IIS7.
推荐答案
尝试
Process excelProcess = Process.GetProcessesByName("EXCEL")[0];
if (!excelProcess.CloseMainWindow())
{
excelProcess.Kill();
}
这篇关于互操作的Excel不关闭过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!