0

I am writing an ASP.Net application that is going to have the ability to export data to an excel document after writing.

Writing to the file is fine and works using Microsoft.Office.Interop.Excel however when after writing the file 1 or 100 times there is always one or more lingering process that simply will not go unless I open a command window and run tskill excel.

the code I am using is as follows

Protected Sub writeToExcel_Click(sender As Object, e As EventArgs) Handles writeToExcel.Click


    Dim objExcel As New Excel.Application()
    Dim objWorkbook As Excel.Workbook = Nothing
    Dim objSheet As Excel.Worksheet = Nothing
    objWorkbook = objExcel.Workbooks.Open(Server.MapPath("Files\testExcel.xlsx"))
    objSheet = objWorkbook.Worksheets(1)

    ''this will find the last used row
    Dim last As Excel.Range = objSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing)

    Dim lastUsedRow As Integer = last.Row
    Dim lastUsedColumn As Integer = last.Column

    objSheet.Cells(lastUsedRow + 1, 1).value = "55555"
    'objExcel.AlertBeforeOverwriting = false;
    objExcel.DisplayAlerts = False
    Dim newFile As String = String.Format("Files\testExcel-{0}.xlsx", DateTime.Now.ToString("yyyyMMdd"))
    Dim path As String = Server.MapPath(newFile)
    Try
        objWorkbook.Save()
        objWorkbook.Close()
        objExcel.Application.Quit()
        ReleaseObject(objSheet)
        ReleaseObject(objWorkbook)
        ReleaseObject(objExcel)
    Catch ex As Exception
        Console.Write(ex.Message)
        Console.Write(ex.StackTrace)
        Console.ReadKey()
    End Try


End Sub

Private Sub ReleaseObject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

I have seen a number of solutions on stack, none of which have worked for me at the moment

such as this one

previous answer

another previous

and this one too which stack wont allow me to create as a link Excel application not quitting after calling quit

any and all help would very much be appreciated, as when this goes live, I don't want to run into out of memory exceptions if one or more processes linger.

Community
  • 1
  • 1
Simon Price
  • 3,011
  • 3
  • 34
  • 98
  • Could you release the range as well? – Andrew Mortimer Jan 22 '16 at 14:58
  • Is your application a web project? The easiest solution to your problem is to not use the Interop, and if you're going to be on a server, the Interop thing gets more important as the Office Interops are not designed to run in a web environment. – krillgar Jan 22 '16 at 14:59
  • @krillgar yes, this is a web project. Would ClosedXML.Excel be better in this instance? If so, how simple would it be for me to direct where I want the writing to start like I am here – Simon Price Jan 22 '16 at 15:01
  • I would advice not to use the excel.interop in any situation. There are better libraries: http://npoi.codeplex.com/ and http://epplus.codeplex.com/ – RvdK Jan 22 '16 at 15:01
  • I would recommend EPPlus like @RvdK. It has a lot more functionality than Closed XML has (images, charts, etc). There are plenty of examples on their site. It is very easy to work with, and you have ZERO issues with COM components. – krillgar Jan 22 '16 at 15:03
  • I have the following code sample https://code.msdn.microsoft.com/Basics-of-using-Excel-4453945d which explains how to properly creat and dispose of excel objects and this https://code.msdn.microsoft.com/Excel-get-used-rows-and-15b43cb7 shows the same but focuses on getting last row and col. – Karen Payne Jan 22 '16 at 15:05
  • I like the ComObjectManager class as in this answer: http://stackoverflow.com/questions/2926205/does-every-excel-interop-object-need-to-be-released-using-marshal-releasecomob – GinjaNinja Jan 22 '16 at 15:18
  • Have changed the library to one that can be disposed of as per your recommendations. thank you for your help – Simon Price Jan 25 '16 at 09:52

0 Answers0