在此问题由于重复而被关闭之前,我尝试了这些答案。
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
但是他们都没有为我工作,我也尝试过从网上查找。
Opening an excel file leaves "EXCEL.EXE" process open(以及许多其他),但该过程仍然存在。
此软件将是自动上载程序,它将在计算机启动时运行。然后它将检查计算机的Drive D分区,并将其中的所有excel上载。我已经使用它上传了CSV文件,但从未遇到过此类问题,但是,现在,我正在尝试上传Excel文件。
我想知道如何在不使用“ KILL”命令的情况下正确终止该进程。我也尝试使用process.kill
,但是当我从计算机打开另一个excel工作簿时,该excel被杀死,而不是被上传。
请注意,此代码已经过某种修改以仅输出excel的内容,它已经可以工作了,我只想知道如何关闭EXCEL PROCESS,因为该系统有时可能会上传数百个excel工作表。
我在下面使用了这段代码:
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
编辑:
我忘记添加ExcelProcessInit()
和ExcelProcessKill()
的代码
这是他们的代码:
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