2012-08-14 45 views
2

我在後臺離開excel殭屍進程的程序出現問題。我已經遵循了所有的建議和例子,以及試圖避免這種情況的MSDN,但這讓我感到非常緊張。任何人都可以發現是什麼導致殭屍進程發生?(另一種).net Excel殭屍問題

Imports System 
Imports System.Collections 
Imports System.IO 
Imports Excel = Microsoft.Office.Interop.Excel 

私人小組LoadExcelButton_Click(BYVAL發件人爲System.Object的,BYVALË作爲System.EventArgs)把手LoadExcelButton.Click

Dim xlApp As New Excel.Application 
    Dim xlBook As Excel.Workbook = Nothing 'instantiated to nothing to help try and avoid zombies 
    Dim xlSheet As Excel.Worksheet = Nothing 
    Dim STFRange As Excel.Range = Nothing 

    Dim Name As String, Easting As Integer, Northing As Integer, tDSpa As Double, Type As String 

    Dim NumberSTF As Integer, NumberProperties As Integer, i As Integer, ExcelPath As String 

    ExcelPath = Me.ExcelPathTextBox.Text.ToString 

    If File.Exists(ExcelPath) = False Then 
     MessageBox.Show("Excel file does not exist, exiting.") 
     Exit Sub 
    End If 

    Try 

     xlApp.Visible = False 

     xlBook = xlApp.Workbooks.Open(ExcelPath) ', , [ReadOnly]:=True 

     xlSheet = xlBook.Sheets("STF") 

     NumberSTF = xlSheet.UsedRange.Rows.Count - 1 '-1 to account for header 
     NumberProperties = xlSheet.UsedRange.Columns.Count 

     'create a new collection 
     'http://msdn.microsoft.com/en-us/library/xth2y6ft(v=vs.71).aspx 
     Dim mySTFCollection As New STFCollection 

     For i = 1 To NumberSTF 'rather than a for each loop which would require more excel ranges 

      STFRange = xlSheet.Cells(i + 1, 1) '+1 on row to account for header 
      Name = STFRange.Value.ToString 

      STFRange = xlSheet.Cells(i + 1, 2) 
      Easting = CInt(STFRange.Value) 

      STFRange = xlSheet.Cells(i + 1, 3) 
      Northing = CInt(STFRange.Value) 

      STFRange = xlSheet.Cells(i + 1, 4) 
      tDSpa = CDbl(STFRange.Value) 

      STFRange = xlSheet.Cells(i + 1, 5) 
      Type = STFRange.Value.ToString 

      Dim objSTF As New STF(Name, Easting, Northing, tDSpa, Type) 

      mySTFCollection.Add(objSTF) 

     Next i 

     GC.Collect() 
     GC.WaitForPendingFinalizers() 
     GC.Collect() 
     GC.WaitForPendingFinalizers() 

     ReleaseObject(STFRange) 
     STFRange = Nothing 
     ReleaseObject(xlSheet) 
     xlSheet = Nothing 

     xlBook.Close(True, ,) 
     ReleaseObject(xlBook) 
     xlBook = Nothing 

     xlApp.Quit() 
     ReleaseObject(xlApp) 
     xlApp = Nothing 

    Catch ex As Exception 

     MessageBox.Show(ex.Message) 

    End Try 
End Sub 

Public Sub ReleaseObject(ByVal obj As Object) 

    Try 
     Marshal.ReleaseComObject(obj) 
     obj = Nothing 
    Catch ex As Exception 
     obj = Nothing 
    Finally 
     GC.Collect() 
    End Try 
End Sub 
+1

無法完成。你沒有按照它的意圖使用excel。 Excel是一個桌面應用程序,互操作是這種怪癖的獎金。這就是爲什麼MSDN建議避免它,而不是因爲他們是一羣混蛋:p – banging 2012-08-17 19:42:24

+0

這一定是可能的,我不敢相信。我已經在VBA中完成了從ESRI ArcGIS到Excel的工作,但是無法從VB.net開始工作 – sarkyscouser 2012-09-10 12:50:22

回答

0

第一第一

If File.Exists(ExcelPath) = False Then 
    MessageBox.Show ("Excel file does not exist, exiting.") 
    Exit Sub 
End If 

應該外面的東西Try - End Try並在實例化您的Excel對象之前。如果路徑不存在,則指示代碼退出該子節點而不進行清理。最後使用GCcollect

這是我最喜歡的處理它

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) _ 
Handles Button1.Click 
     ' 
     '~~> Rest of code 
     ' 

     '~~> Close workbook and quit Excel 
     xlWb.Close (False) 
     xlApp.Quit() 

     '~~> Clean Up 
     releaseObject (xlApp) 
     releaseObject (xlWb) 
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 

如果你是熱衷於從VB.net Excel自動化,那麼我會建議你去通過這個link方式。

隨訪

使用此代碼。

Private Sub LoadExcelButton_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    Dim Name As String = "", Type As String = "", ExcelPath As String = "" 
    Dim Easting As Integer = 0, Northing As Integer = 0 
    Dim NumberSTF As Integer = 0, NumberProperties As Integer = 0, i As Integer = 0 
    Dim tDSpa As Double = 0 

    ExcelPath = Me.ExcelPathTextBox.Text.ToString 

    If File.Exists(ExcelPath) = False Then 
     MessageBox.Show("Excel file does not exist, exiting.") 
     Exit Sub 
    End If 

    Dim xlApp As New Excel.Application 
    Dim xlBook As Excel.Workbook = Nothing 
    Dim xlSheet As Excel.Worksheet = Nothing 
    Dim STFRange As Excel.Range = Nothing 

    xlApp.Visible = True 

    xlBook = xlApp.Workbooks.Open(ExcelPath) 

    xlSheet = xlBook.Sheets("STF") 

    NumberSTF = xlSheet.UsedRange.Rows.Count - 1 
    NumberProperties = xlSheet.UsedRange.Columns.Count 

    'create a new collection 
    'http://msdn.microsoft.com/en-us/library/xth2y6ft(v=vs.71).aspx 
    Dim mySTFCollection As New STFCollection 

    For i = 1 To NumberSTF 

     STFRange = xlSheet.Cells(i + 1, 1) 
     Name = STFRange.Value.ToString 

     STFRange = xlSheet.Cells(i + 1, 2) 
     Easting = CInt(STFRange.Value) 

     STFRange = xlSheet.Cells(i + 1, 3) 
     Northing = CInt(STFRange.Value) 

     STFRange = xlSheet.Cells(i + 1, 4) 
     tDSpa = CDbl(STFRange.Value) 

     STFRange = xlSheet.Cells(i + 1, 5) 
     Type = STFRange.Value.ToString 

     Dim objSTF As New STF(Name, Easting, Northing, tDSpa, Type) 

     mySTFCollection.Add(objSTF) 

    Next i 

    '~~> Close the File 
    xlBook.Close(False) 

    '~~> Quit the Excel Application 
    xlApp.Quit() 

    '~~> Clean Up 
    releaseObject(xlSheet) 
    releaseObject(xlBook) 
    releaseObject(xlApp) 
End Sub 

'~~> Release the objects 
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 
+0

謝謝我明天會試試這個 – sarkyscouser 2012-08-15 16:49:37

+0

好的,所以我已經嘗試過了,並沒有什麼區別,我仍然有一個殭屍坐在那裏記憶。 – sarkyscouser 2012-08-17 13:42:50

+0

你能否更新你現在在你的問題中使用的確切代碼 – 2012-08-17 13:47:57

0

而不是在您的Excel應用程序調用Close()的,你嘗試過Dispose()

您可能想嘗試在代碼的Finally塊中進行清理。

+0

從我所能看到的沒有可用的處理方法 – sarkyscouser 2012-09-07 09:18:26