【发布时间】:2013-02-21 16:47:19
【问题描述】:
我已经为这个问题苦苦挣扎了几天,我已经进行了研究并应用了我在各个论坛上找到的所有建议,但我仍然无法解决它。
我的问题是使用互操作库的 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;
}
如您所见,我正在关闭并释放所有对象,但应用程序并未退出。
我正在使用 IIS7 的 32 位 Windows Server 2008(生产)和 Windows 7(开发)中进行测试。
【问题讨论】:
-
您不能使用来自 ASP.NET 或其他服务器技术的 Office Interop。见Considerations for server-side Automation of Office
-
@JohnSaunders:你可以,这只是一个非常非常糟糕的主意 ;-)
-
@StingyJack:不,那不是 asp.net 造成的。
-
@roma8716:正如 John 指出的那样,使用 ASP.Net 的互操作(根本)不是一个好主意。我建议您要么使用适合此任务的组件(例如epplus.codeplex.com),要么编写一个接受排队请求以处理 Excel 文件的 Windows 服务。
标签: c# asp.net visual-studio-2010 excel office-interop