2013-11-28 57 views
0

我有一个托管在IIS中的网站。 我的应用程序是关于写入和从Excel文档读取数据的。在IIS中托管asp.net网站时清洁Com组件

当我从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总是有的,但我怎么样无法清洗。 请在这方面帮助我。

+0

这是从ASP.NET或其他服务器技术使用Office互操作一个可怕的想法后创建新EXCEL.EXE 。这些API被编写用于桌面应用程序,用于自动化Office(一套桌面应用程序)。服务器应用程序在许多方面有所不同,因此在其中使用Office Interop是非常非常糟糕的主意。它也不受Microsoft的支持,并可能违反您的Office许可证。请参阅[服务器端自动化Office的注意事项](http://support.microsoft.com/kb/257757) –

+0

是的,我转移到打开XML,感谢您的建议。 – Ram

回答

-1

保存好excel后应关闭工作簿。因为每次你创建NEA app.application将节省Excel中使用下面的代码

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

首先'ex.Message'没有足够的信息进行故障排除。使用'ex.ToString()'。更重要的是,你的代码在显示消息后要做什么? -1用于忽略异常。 –

+0

好的,先生。我只是试图帮助关闭excel对象。他已经放置了try catch块,所以我认为他不需要使用我的try catch。对不起,下次我会照顾 –

+0

谢谢你的回答,但是当多个用户试图在excel上执行写/读操作时,excel进程将被终止,它将无法工作。 – Ram