rom*_*716 4 c# asp.net excel visual-studio-2010 office-interop
我已经在这个问题上苦苦挣扎了几天,我已经进行了研究并应用了我在各种论坛上发现的所有建议,但我仍然无法解决它.
我的问题是excel使用互操作库,我有一个excel文件用作模板,所以我打开它并保存在一个新名称的新位置.一切都很好,除了Excel进程在创建和关闭文件后继续运行.
这是我的代码
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;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在关闭并释放所有对象,但应用程序不会退出.
我正在使用IIS7在32位的Windows Server 2008(生产)和Windows 7(开发)中进行测试.
小智 6
尝试
Process excelProcess = Process.GetProcessesByName("EXCEL")[0];
if (!excelProcess.CloseMainWindow())
{
excelProcess.Kill();
}
Run Code Online (Sandbox Code Playgroud)
use*_*261 -2
oWB.Close(false, strCurrentDir + strFile, Type.Missing);
oWB.Dispose();
wbks.Close();
wbks.Dispose();
oXL.Quit();
oXL.Dispose();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8252 次 |
最近记录: |