Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Excel Object not releasing. VB.net 2012

$
0
0

so I'm using vb.net to transfer data from a excel file to list view. Everything works out fine i.e the importing part but i cant seem to release the excel object as excel.exe is still in task manager after the importing is done.

Below is the code i use

Private Function ExcelSheet(ByVal lst As ListView, ByVal c As Integer)
        Dim Completed As Boolean = False
        Dim xlsApp As Excel.Application
        Dim xlsWB As Excel.Workbook
        Dim xlsSheet As Excel.Worksheet
        Dim columnCount As Integer = 0
        Dim rowcount As Integer = 0
        Dim i As Integer = 0

        xlsApp = New Excel.Application
        xlsApp.Visible = False
        xlsApp.DisplayAlerts = False

        xlsWB = xlsApp.Workbooks.OpenXML(dlgOpen.FileName)
        xlsSheet = xlsWB.Worksheets(c)
        xlsSheet.Select()
        columnCount = xlsSheet.UsedRange.Columns.Count
        rowcount = xlsSheet.UsedRange.Rows.Count - 1

        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = rowcount

        Do Until lst.Columns.Count = xlsSheet.UsedRange.Columns.Count
            lst.Columns.Add(xlsSheet.Cells(1, i + 1).Value)
            i = i + 1
        Loop

        Dim lvi As New ListViewItem
        Dim x As Integer = 2
        Dim t As Integer = 1
        Dim lvwItem As New ListViewItem()
        Dim d As Integer

        Do Until lst.Items.Count = rowcount
            d = 1
            Do Until d = lst.Columns.Count + 1

                If d = 1 Then

                    If xlsSheet.Cells(x, d).value = vbNullString Then
                        lvwItem = lst.Items.Add(vbNullString)
                    Else
                        lvwItem = lst.Items.Add(xlsSheet.Cells(x, d).value)
                    End If

                Else
                    If xlsSheet.Cells(x, d).value = vbNullString Then
                        lvwItem.SubItems.Add(vbNullString)
                    Else
                        lvwItem.SubItems.Add(xlsSheet.Cells(x, d).value)
                    End If
                End If
                d = d + 1

            Loop
            x = x + 1
            ProgressBar1.Value = lst.Items.Count
            lblStatus.Text = (Math.Round((lst.Items.Count.ToString / rowcount), 1) * 100) & "%"

            For e = 1 To lst.Columns.Count - 1
                If lstComputers.Columns(e).Name = "Ports" Then
                    lst.AutoResizeColumn(e, ColumnHeaderAutoResizeStyle.ColumnContent)
                Else
                    lst.AutoResizeColumn(e, ColumnHeaderAutoResizeStyle.HeaderSize)
                End If
            Next
        Loop

        xlsApp.DisplayAlerts = True

        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlsSheet)
        xlsSheet = Nothing
        xlsWB.Close(SaveChanges:=False)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlsWB)
        xlsWB = Nothing
        xlsApp.Workbooks.Close()
        xlsApp.Quit()
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlsApp)
        xlsApp = Nothing

        Return Completed
    End Function


I am importing a 4 sheet workbook. but for testing purposes i tried just importing one and it still has the same issue. Although just using the code below from a button click event works fine with excel.exe closing as it should.

Dim xlsapp As New Excel.Application
xlsapp.Quit()      
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsapp)

I'm guessing I'm not releasing everything properly in order for excel.exe to close. I'm a noob vb.net coder so appreciate any form of help.

Thanks


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>