16,721
社区成员




'关闭Excel应用,重要!!内存释放
wbXL = Nothing
shXL = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(books)
appXl.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(appXl)
System.Threading.Thread.Sleep(1000)
Catch ex As Exception
Debug.Print("Exception Found: " + xmlFile + " would be closed!!")
Debug.Print(ex.Message + vbNewLine + ex.StackTrace)
End Try
Sub LoadFile(ByVal xmlFile As String)
'读取Excel必须
Dim appXl As New Application
Dim books As Workbooks = appXl.Workbooks
Dim wbXL As Workbook
Dim shXL As Worksheet
Try
appXl.DisplayAlerts = False
Debug.Print("Alerts Init")
appXl.Visible = False
Debug.Print("Visible Init")
wbXL = books.Open(xmlFile)
Debug.Print("Open Completed!")
shXL = wbXL.Sheets("Test Item")
Debug.Print("Sheets Test Item Completed!")
'Lot Info区域获取开始
'Lot Info 区域行数
Debug.Print("Init Completed!")
'省略其余代码
Catch ex As Exception
Debug.Print("Exception Found: " + xmlFile + " would be closed!!")
Debug.Print(ex.Message + vbNewLine + ex.StackTrace)
Finally
If shXL IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(shXL)
End If
If wbXL IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbXL)
End If
books.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject(books)
appXl.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(appXl)
End Try
End Sub
这样看看报不报错,如果报错的话看下message和stacktrace。Try
....
Catch ex As Exception
Debug.Print("Exception Found: " + xmlFile + " would be closed!!")
Finally
appXL.Quit()
End Try
前面没仔细看代码,这样试试。