Before this question gets closed because its a duplicate, I tried these answers.
Excel Process not closing in VB.net
Application not quitting after calling quit
VB.NET Excel Program Leaves EXCEL.EXE floating after completion
Excel process still runs after closing in VB.net
But none of them worked for me, I also tried looking from the web.
Opening an excel file leaves "EXCEL.EXE" process open (and plenty of others) But the process still remains.
This software will be a automatic uploader that will run when the computer boots up. then it will check the Drive D partition of the computer and will upload all excel in it. I have already used this to upload CSV files and never had such problems, however, now I am trying to upload Excel files.
I want to know how can I properly kill the process without using the "KILL" command. I also tried to use process.kill
but when I open another excel workbook from the computer, that excel gets killed, not the excel being uploaded.
Do take note that this code is somehow modified to simply output the contents of the excel, it works already, I just want to know how to close the EXCEL PROCESS since there might be a time that this system will upload hundreds of excel worksheets.
I used this code below:
Sub uploadExcelFiles(ByVal locs As String)
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim location As String
Dim isData As Boolean = False
Dim m_CountTo As Integer = 0
filename = ""
globalloc = ""
'Gets Files from Folders
Dim counting As Integer = 0
Dim csvcount As String() = Directory.GetFiles(locs, "*.xls")
Dim ToUploadCSV As String
ExcelProcessInit()
For counting = 0 To csvcount.Count - 1
CheckListofCSV.Clear()
filename = ""
location = csvcount(counting).ToString
globalloc = location
ToUploadCSV = Path.GetFileName(location).ToString
Dim ListLines As New List(Of String)
Dim ListLinesNEW As New List(Of String)
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open(location)
xlWorkSheet = xlWorkBook.Worksheets("Sheet1")
'Use this code to read the data from EXCEL
'**DATA HEADER**
MsgBox(xlWorkSheet.Cells(2, 6).value)
MsgBox(xlWorkSheet.Cells(2, 7).value)
MsgBox(xlWorkSheet.Cells(2, 8).value)
MsgBox(xlWorkSheet.Cells(3, 2).value)
MsgBox(xlWorkSheet.Cells(3, 5).value)
MsgBox(xlWorkSheet.Cells(3, 8).value)
MsgBox(xlWorkSheet.Cells(4, 2).value)
MsgBox(xlWorkSheet.Cells(4, 5).value)
MsgBox(xlWorkSheet.Cells(4, 7).value)
MsgBox(xlWorkSheet.Cells(4, 8).value)
MsgBox(xlWorkSheet.Cells(62, 2).value)
'**DATA HEADER**
'**DATA DETAILS**
'ROWS
Dim i As Integer = 5
'COLUMNS
Dim col As Integer = 2
'SAMPLING COUNT FROM EXCEL
Dim SampQTYXcel As Integer = 0
Do While Len(xlWorkSheet.Cells(i, col).value) > 0
For i = 5 To 10
MsgBox(xlWorkSheet.Cells(i, col).value)
Next
'This is sampling QTY
getSamplingQty(xlWorkSheet.Cells(3, 2).value, xlWorkSheet.Cells(5, col).value, xlWorkSheet.Cells(3, 8).value)
'Sample Data 10 onwards
Do While Len(xlWorkSheet.Cells(i, col).value) > 0
MsgBox(xlWorkSheet.Cells(i, col).value)
i = i + 1
SampQTYXcel = SampQTYXcel + 1
Loop
'Excel data has more data than Database QTY then REPORT
If SampQTYXcel > SamplQTY Then
'SEND ERROR
MsgBox("ERROR")
End If
'Add 1 to move to next column
col = col + 1
i = 5
SampQTYXcel = 0
Loop
xlWorkBook.Save()
'**DATA DETAILS**
xlApp.DisplayAlerts = False
xlWorkBook.Close()
xlApp.Quit()
xlApp = Nothing
xlWorkBook = Nothing
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
releaseObject(xlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
ExcelProcessKill()
Next
End Sub
EDIT:
I forgot to add the codes of ExcelProcessInit()
and ExcelProcessKill()
Here are their codes:
Private Sub ExcelProcessInit()
Try
'Get all currently running process Ids for Excel applications
mExcelProcesses = Process.GetProcessesByName("EXCEL")
Catch ex As Exception
End Try
End Sub
Private Sub ExcelProcessKill()
Dim oProcesses() As Process
Dim bFound As Boolean
Try
'Get all currently running process Ids for Excel applications
oProcesses = Process.GetProcessesByName("EXCEL")
If oProcesses.Length > 0 Then
For i As Integer = 0 To oProcesses.Length - 1
bFound = False
For j As Integer = 0 To mExcelProcesses.Length - 1
If oProcesses(i).Id = mExcelProcesses(j).Id Then
bFound = True
Exit For
End If
Next
If Not bFound Then
oProcesses(i).Kill()
End If
Next
End If
Catch ex As Exception
End Try
End Sub