locked
Excel problem RRS feed

  • Question

  • User-189004138 posted

    I m operning excel file using Applicaion object of Excel,Interop

    Now i am closing it but it doesn't release from memory

    It's process is runnig like Excel.exe

    How can i release it.

    Monday, August 2, 2010 6:05 AM

All replies

  • User1643008609 posted

    Hi Friend,

    Excel app object is Com component so to release we need to execute special code

    // Need all following code to clean up and extingush all references!!!
    oWB.Close(null,null,null);
    oXL.Workbooks.Close();
    oXL.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
    oSheet=null;
    oWB=null;
    oXL = null;
    GC.Collect(); // force final cleanup!

    http://www.eggheadcafe.com/articles/20021012.asp

    http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c

    prasadP

    Monday, August 2, 2010 6:28 AM
  • User-624088818 posted

    Try below

    Marshal.ReleaseComObject(excelobject);
    excelobject=null;

    Monday, August 2, 2010 7:01 AM
  • User-189004138 posted

     It's not work for me

     any other way.


    Wednesday, August 4, 2010 2:09 AM
  • User2022958948 posted

    Could you please post some code what you used? 

    Friday, August 6, 2010 4:24 AM
  • User-189004138 posted

    oWB.Close(null, null, null);
                    oXL.Workbooks.Close();
                    oXL.Quit();
                    Marshal.ReleaseComObject(oRng);

                    Marshal.ReleaseComObject(oXL);

                    Marshal.ReleaseComObject(oSheet);

                    Marshal.ReleaseComObject(oWB);



                    try
                    {
                        oSheet = null;
                        oWB = null;
                        oXL = null;
                        GC.Collect();
                        // GC.WaitForPendingFinalizers();

                    }
                    catch
                    {

                    }

    Saturday, August 28, 2010 3:25 AM
  • User793200426 posted

    Hi,

    if you want to avoid unmanaged resource issues and work with completely managed code that is not using Excel Interop to manipulate Excel documents, I recommend you take a look at this Excel .NET library.

    Here is a sample Excel ASP.NET code that exports DataTable as Excel document to ASP.NET output stream:

    // Get DataTable that DataGrid is bound to.
    var dataTable = (DataTable)dataGrid.DataSource;
    
    // Create new ExcelFile.
    var ef = new ExcelFile();
    // Add new worksheet to the file.
    var ws = ef.Worksheets.Add(dataTable.TableName);
    // Insert the data from DataTable to the worksheet starting at cell "A1".
    ws.InsertDataTable(dataTable, "A1", true);
    
    // Stream file to browser.
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
    ef.SaveXls(Response.OutputStream);
    Response.End();


    Tuesday, August 31, 2010 4:39 AM