在IIS中托管asp.net网站时清理Com组件

时间:2013-11-28 06:12:26

标签: c# asp.net excel iis

我在IIS中托管了一个网站。 我的应用程序是关于在Excel文档中编写和读取数据。

当我从VS运行应用程序时,我一直只看到一个EXCEL.exe 但是当我在IIS中托管相同的应用程序时,每次写入它创建一个excel对象并在任务管理器中显示,因此有时我的网站会停止响应。

以下是我的代码

    try
            {
                xlApp = new app.Application();
                xlWorkBooks = xlApp.Workbooks;          
                xlWorkBook = xlWorkBooks.Open(FilePath, missing, false, missing, missing, missing, true, missing, missing, true, missing, missing, missing, missing, missing);   
                xlWorkSheet = xlWorkBook.Worksheets.get_Item(index);
//code …………………………….
                xlApp.DisplayAlerts = false;
                xlWorkBook.Save();
                boolWriteSuccess = true;
                xlApp.DisplayAlerts = true;
            }
Catch()
{
}            
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                Marshal.FinalReleaseComObject(xlWorkSheet);
                Marshal.FinalReleaseComObject(xlWorkSheets);
                xlWorkBook.Close(Type.Missing, Type.Missing, Type.Missing);
                Marshal.FinalReleaseComObject(xlWorkBook);
                xlWorkBooks.Close();
                Marshal.FinalReleaseComObject(xlWorkBooks);
                if (xlApp != null)
                    xlApp.Quit();
                Marshal.FinalReleaseComObject(xlApp);
            }

以下是IIS中托管网站时任务管理器的屏幕截图 enter image description here

我想从任务管理器中清除excel.exe,但有些我无法清理。 请帮助我。

1 个答案:

答案 0 :(得分:-1)

保存excel后,您应该关闭工作簿。因为每次创建nea app.application时都会创建新的excel.exe 保存excel后使用下面的代码

    try {
foreach (Process proc in Process.GetProcessesByName("Excel"))
        {
            proc.Kill();
        }}catch(Exception ex){
MessageBox.Show(ex.Message);}