locked
Excel.exe still remains in memory - ASP.net web development RRS feed

  • Question

  • User-735764652 posted

    Hi,

    I have been trying to find a solution to my problem for a lot of hours but couldn't succeed. I have been working on a new website of mine which simply reads an excel file and displays the results on the screen for the time being. However, after i have successfully read the excel from asp.net code and displayed the data, and after i close the program, the excel.exe wont leave the task manager . I have tried microsoft solution to recreate the problem and their solution works but when i try to use their code into my program it doesnt work. The only code that is different from their solution is only one line ! Below i have pasted their solution :

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim dbConnection As SqlConnection
    dbConnection = New SqlConnection(System.Configuration.ConfigurationMa nager.AppSettings.Get("ConnectionString"))
    Dim filepath As String = System.Configuration.ConfigurationManager.AppSetti ngs.Get("SourcefilePath")
    Dim filename As String = System.Configuration.ConfigurationManager.AppSetti ngs.Get("SourceFileName")
    Dim completepathname As String
    completepathname = filepath & filename

    Dim oApp As New Excel.Application()
    Dim oBooks As Excel.Workbooks = oApp.Workbooks
    Dim oBook As Excel.Workbook = oBooks.Add
    'Dim oBook As Excel.Workbook = oApp.Workbooks.Open(completepathname)
    Dim oSheet As Excel.Worksheet = oApp.ActiveSheet

    NAR(oSheet)
    oBook.Close(False)
    NAR(oBook)
    NAR(oBooks)
    oApp.Quit()
    NAR(oApp)



    Label5.Text = "Sleeping..."
    System.Threading.Thread.Sleep(5000)
    Label5.Text = "End Excel"

    End Sub


    A simple program that opens an excel and then closes it . This works BUT in this code it doesnt not specify the filename of the excel. So i tried to modify one line to add the filename. My code that is NOT working (with only 1 line modified ) is shown below :

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim dbConnection As SqlConnection
    dbConnection = New SqlConnection(System.Configuration.ConfigurationMa nager.AppSettings.Get("ConnectionString"))
    Dim filepath As String = System.Configuration.ConfigurationManager.AppSetti ngs.Get("SourcefilePath")
    Dim filename As String = System.Configuration.ConfigurationManager.AppSetti ngs.Get("SourceFileName")
    Dim completepathname As String
    completepathname = filepath & filename

    Dim oApp As New Excel.Application()
    Dim oBooks As Excel.Workbooks = oApp.Workbooks
    'Dim oBook As Excel.Workbook = oBooks.Add
    Dim oBook As Excel.Workbook = oApp.Workbooks.Open(completepathname)
    Dim oSheet As Excel.Worksheet = oApp.ActiveSheet

    NAR(oSheet)
    oBook.Close(False)
    NAR(oBook)
    NAR(oBooks)
    oApp.Quit()
    NAR(oApp)



    Label5.Text = "Sleeping..."
    System.Threading.Thread.Sleep(5000)
    Label5.Text = "End Excel"

    End Sub


    As you can see, the only lines that differs is the one with the filename. What i am doing wrong and how i can modify their code to include a filename and actually open an excel file i want ? Please help me .

    Thank you in advance

    Friday, January 29, 2010 1:51 AM

All replies

  • User373202871 posted
    oApp.Workbooks.Close()
    oApp.Quit()                
    
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp)
    oApp = Nothing
    
    ' Clean up memory so Excel can shut down.
    GC.Collect()
    GC.WaitForPendingFinalizers()


     

    Friday, January 29, 2010 2:42 AM
  • User-735764652 posted

    Thank you for your reply. I dont understand the difference from your code. the 'NAR' function does exactly that ( the marshal release object) .

     

    NAR(oSheet)
    oBook.Close(False)
    NAR(oBook)
    NAR(oBooks)
    oApp.Quit()
    NAR(oApp)
     

    Friday, January 29, 2010 4:25 AM
  • User373202871 posted

    well if thats the case, you may want to add the garbage collector (GC) code.

    works fine on me.

    Friday, January 29, 2010 4:33 AM
  • User-735764652 posted

    So if you make a new web application aSP.net , and place a simple button to execute my code with the opening of a specific excel file. Does the excel process disapears? What am i doing different ? While testing i am using the debugger with the studio development server also , i dont know if that helps 

    Friday, January 29, 2010 5:15 AM