0

I have a ASP.net application on IIS. In this app I created a Excel application:

using Excel = Microsoft.Office.Interop.Excel;
public Excel.Application ExlApp;
//
public Excel.Workbook ExlWb;
//
public Excel.Worksheet ExlWs;

During the application work, I created a new workbooks and in finally, I destroy my objects:

        GC.Collect();
        GC.WaitForPendingFinalizers();

        Marshal.ReleaseComObject(ExlWs);        

        ExlWb.Close(Type.Missing, Type.Missing, Type.Missing);
        Marshal.ReleaseComObject(ExlWb);

The problem: during the work, the EXCEL proces is growing up in memory, how can I close the workbooks correct?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Oleg
  • 1,467
  • 4
  • 26
  • 39
  • 1
    can you show the code where you are creating the objects .. also not sure why you are calling GC.Collect.. when all you need to do is Marshal.RealeaseComObject(....) wondering if you are creating this in a loop or not.. – MethodMan Feb 06 '12 at 17:42
  • 1
    See http://stackoverflow.com/questions/7382704/asp-net-web-service-using-office-2010-com. You can't use office automation from a server process. – John Saunders Feb 06 '12 at 17:47
  • You're not even legally allowed to do this - if you ignore all of the warnings - unless you've got Office licenses for all the users of your web application. – x0n Feb 06 '12 at 17:58
  • I need the office for generate reports...And there are troubles with disposing objects( – Oleg Feb 06 '12 at 18:06
  • "using Automation from Windows Service (IIS and ASP.NET for example) is NOT supported by MS " - is it possible to use Excel COM objects in winform app with Automation support ? – Oleg Feb 06 '12 at 18:13
  • 1
    Yes, a windows forms application would be able to use Office automation. If you still want to generate Excel reports server side, have a look at this article: http://msdn.microsoft.com/en-us/library/hh180830.aspx – granaker Feb 07 '12 at 07:31

1 Answers1

5

Maybe you should look at replacing the Excel automation with OpenXML processing. Automating office applications server side is an unsupported scenario:

http://support.microsoft.com/kb/257757

"Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment."

granaker
  • 1,318
  • 8
  • 13