2016-10-28 41 views
1

關閉我已經創建,創建在VB.NET的Excel在VB.NET

幾個Excel電子表格的應用程序後仍保留在任務管理器中打開

問題我遇到的是,我不能讓Excel將完全放棄。

我創建並填充用Excel VB.NET中的工作簿隱藏進程(Microsoft Excel)中顯示在任務管理器的後臺進程

完成後我做的Excel可見,轉移到Google Apps。

然後,當我關閉Excel時,該過程返回到後臺進程。

任何想法我做錯了什麼?

代碼:

Dim oExcel As Excel.Application = Nothing 
    Dim oWorkbook As Excel.Workbook = Nothing 
    Dim oWorksheet As Excel.Worksheet = Nothing 
    Dim oRange As Excel.Range = Nothing 

    oExcel = CreateObject("Excel.Application") 
    oExcel.DisplayAlerts = False 
    oExcel.Visible = False 

    oWorkbook = oExcel.Workbooks.Add 
    oWorksheet = oWorkbook.ActiveSheet 
    'Populate, format, etc. 
    oWorkbook.SaveAs(Me.txtExportLocation.Text & "\sales.xlsx") 

    oExcel.Visible = True 

    oRange = Nothing 
    oWorksheet = Nothing 
    oWorkbook = Nothing 

    ReleaseObject(oExcel) 

    Public Sub ReleaseObject(ByVal obj As Object) 

     Dim iValue As Integer = 0 

     Try 
      Do 
       iValue = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) 
      Loop While iValue > 0 
     Catch ex As Exception 
      RaiseError("", "modGeneral." & "." & System.Reflection.MethodBase.GetCurrentMethod().Name, Err.Number, Err.Description) 
      obj = Nothing 
     Finally 
      GC.Collect() 
     End Try 

    End Sub 

更新2016年10月31日:

好了,現在我真的很困惑。

以低於使用此代碼的建議,我可以得到的Excel完全有一點需要注意戒菸:

 GC.Collect() 
     GC.WaitForPendingFinalizers() 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(oWorksheet) : oWorksheet = Nothing 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(oWorkbook) : oWorkbook = Nothing 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel) : oExcel = Nothing 

然而,我的代碼是創建兩個工作簿。除SQL外,創建每個工作簿的代碼都是相同的。如果用戶勾選chkA,則清理代碼不起作用。如果他們檢查chkB,它確實有效。如果他們同時檢查,它不起作用。我已爲此低於全碼:

Private Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click 

    Dim oExcel As Excel.Application = Nothing 
    Dim oWorkbook As Excel.Workbook = Nothing 
    Dim oWorksheet As Excel.Worksheet = Nothing 
    Dim drSystem As SqlClient.SqlDataReader = Nothing 
    Dim sSQL As String = "" 
    Dim iRowCount As Integer = 2 

    Try 
     If Not Me.chkA.Checked And Not Me.chkB.Checked Then 
      MsgBox("Select A, B or both before continuing.", vbInformation) 
      Exit Try 
     End If 

     Me.Cursor = Cursors.WaitCursor 
     Me.lblStatus.Text = "Exporting sales..." 

     oExcel = CreateObject("Excel.Application") 
     oExcel.DisplayAlerts = False 
     oExcel.Visible = False 

     If Me.chkA.Checked Then 
      oWorkbook = oExcel.Workbooks.Add 
      oWorksheet = oWorkbook.ActiveSheet 
      oWorksheet.Cells(1, 1).Value = "Ship date" 
      oWorksheet.Cells(1, 2).Value = "Customer" 
      oWorksheet.Cells(1, 3).Value = "Invoice" 
      oWorksheet.Cells(1, 4).Value = "Purchase order" 
      oWorksheet.Cells(1, 5).Value = "Railcar" 
      oWorksheet.Cells(1, 6).Value = "Weight" 
      oWorksheet.Cells(1, 7).Value = "Total" 
      oWorksheet.Cells(1, 8).Value = "Member purchase order" 

      sSQL = "SELECT FORMAT(i.ship_date, N'MM/dd/yyyy') AS ship_date, " 
      sSQL += "i.customer_no, " 
      sSQL += "i.invoice_number, " 
      sSQL += "i.customer_purchase_order_no, " 
      sSQL += "r.railcar_number, " 
      sSQL += "r.weight, " 
      sSQL += "r.total, " 
      sSQL += "i.member + N'-' + i.member_purchase_order_no AS member_purchase_order_no " 
      sSQL += "FROM Invoices i " 
      sSQL += "JOIN Railcars r " 
      sSQL += "ON i.invoice_number = r.invoice_number " 
      sSQL += "WHERE i.ship_date BETWEEN N'" & Format(Me.dtpStartDate.Value, "MM/dd/yyyy") & "' AND N'" & Format(Me.dtpEndDate.Value, "MM/dd/yyyy") & "' AND " 
      sSQL += "invoice_type = N'A' " 
      sSQL += "ORDER BY i.customer_no, " 
      sSQL += "i.ship_date, " 
      sSQL += "r.railcar_number" 
      drSystem = modGeneral.drRunSQL(sSQL, CommandType.Text) 
      Do While drSystem.Read 
       oWorksheet.Cells(iRowCount, 1).Value = drSystem("ship_date") 
       oWorksheet.Cells(iRowCount, 2).Value = drSystem("customer_no") 
       oWorksheet.Cells(iRowCount, 3).Value = drSystem("invoice_number") 
       oWorksheet.Cells(iRowCount, 4).Value = drSystem("customer_purchase_order_no") 
       oWorksheet.Cells(iRowCount, 5).Value = drSystem("railcar_number") 
       oWorksheet.Cells(iRowCount, 6).Value = drSystem("weight") 
       oWorksheet.Cells(iRowCount, 7).Value = drSystem("total") 
       oWorksheet.Cells(iRowCount, 8).Value = drSystem("member_purchase_order_no") 
       iRowCount += 1 
      Loop 
      drSystem.Close() 
      With oWorksheet.Range("A1", "J1") 
       .Font.Bold = True 
       .EntireColumn.AutoFit() 
      End With 
      oWorksheet.Range("D1").EntireColumn.HorizontalAlignment = Excel.Constants.xlLeft 
      With oWorksheet.Range("F1") 
       .EntireColumn.HorizontalAlignment = Excel.Constants.xlRight 
       .EntireColumn.NumberFormat = "#,##0.00_);(#,##0.00)" 
      End With 
      With oWorksheet.Range("G1") 
       .EntireColumn.HorizontalAlignment = Excel.Constants.xlRight 
       .EntireColumn.NumberFormat = "#,##0.00_);(#,##0.00)" 
      End With 
      oWorkbook.SaveAs(Me.txtExportLocation.Text & "\sales-a.xlsx") 
     End If 
     If Me.chkB.Checked Then 
      iRowCount = 2 
      oWorkbook = oExcel.Workbooks.Add 
      oWorksheet = oWorkbook.ActiveSheet 
      oWorksheet.Cells(1, 1).Value = "Ship date" 
      oWorksheet.Cells(1, 2).Value = "Customer" 
      oWorksheet.Cells(1, 3).Value = "Invoice" 
      oWorksheet.Cells(1, 4).Value = "Purchase order" 
      oWorksheet.Cells(1, 5).Value = "Railcar" 
      oWorksheet.Cells(1, 6).Value = "Weight" 
      oWorksheet.Cells(1, 7).Value = "Total" 
      oWorksheet.Cells(1, 8).Value = "Member purchase order" 

      sSQL = "SELECT FORMAT(i.ship_date, N'MM/dd/yyyy') AS ship_date, " 
      sSQL += "i.customer_no, " 
      sSQL += "i.invoice_number, " 
      sSQL += "i.customer_purchase_order_no, " 
      sSQL += "r.railcar_number, " 
      sSQL += "r.weight, " 
      sSQL += "r.total, " 
      sSQL += "i.member + N'-' + i.member_purchase_order_no AS member_purchase_order_no " 
      sSQL += "FROM mxInvoices i " 
      sSQL += "JOIN mxRailcars r " 
      sSQL += "ON i.invoice_number = r.invoice_number " 
      sSQL += "WHERE i.ship_date BETWEEN N'" & Format(Me.dtpStartDate.Value, "MM/dd/yyyy") & "' AND N'" & Format(Me.dtpEndDate.Value, "MM/dd/yyyy") & "' AND " 
      sSQL += "invoice_type = N'B' " 
      sSQL += "ORDER BY i.customer_no, " 
      sSQL += "i.ship_date, " 
      sSQL += "r.railcar_number" 
      drSystem = modGeneral.drRunSQL(sSQL, CommandType.Text) 
      Do While drSystem.Read 
       oWorksheet.Cells(iRowCount, 1).Value = drSystem("ship_date") 
       oWorksheet.Cells(iRowCount, 2).Value = drSystem("customer_no") 
       oWorksheet.Cells(iRowCount, 3).Value = drSystem("invoice_number") 
       oWorksheet.Cells(iRowCount, 4).Value = drSystem("customer_purchase_order_no") 
       oWorksheet.Cells(iRowCount, 5).Value = drSystem("railcar_number") 
       oWorksheet.Cells(iRowCount, 6).Value = drSystem("weight") 
       oWorksheet.Cells(iRowCount, 7).Value = drSystem("total") 
       oWorksheet.Cells(iRowCount, 8).Value = drSystem("member_purchase_order_no") 
       iRowCount += 1 
      Loop 
      drSystem.Close() 
      With oWorksheet.Range("A1", "J1") 
       .Font.Bold = True 
       .EntireColumn.AutoFit() 
      End With 
      oWorksheet.Range("D1").EntireColumn.HorizontalAlignment = Excel.Constants.xlLeft 
      With oWorksheet.Range("F1") 
       .EntireColumn.HorizontalAlignment = Excel.Constants.xlRight 
       .EntireColumn.NumberFormat = "#,##0.00_);(#,##0.00)" 
      End With 
      With oWorksheet.Range("G1") 
       .EntireColumn.HorizontalAlignment = Excel.Constants.xlRight 
       .EntireColumn.NumberFormat = "#,##0.00_);(#,##0.00)" 
      End With 
      oWorkbook.SaveAs(Me.txtExportLocation.Text & "\sales-b.xlsx") 
     End If 
     oExcel.Visible = True 

     GC.Collect() 
     GC.WaitForPendingFinalizers() 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(oWorksheet) : oWorksheet = Nothing 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(oWorkbook) : oWorkbook = Nothing 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel) : oExcel = Nothing 
    Catch ex As Exception 
     RaiseError("", Me.Name & "." & System.Reflection.MethodBase.GetCurrentMethod().Name, Err.Number, Err.Description) 
    Finally 
     If Not drSystem Is Nothing Then 
      If Not drSystem.IsClosed Then drSystem.Close() 
     End If 
    End Try 

    Me.lblStatus.Text = "" 
    Me.Cursor = Cursors.Default 
+0

什麼()' –

+1

和此外,這是[如何正確清理Excel互操作對象?](http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects) –

回答

1

不幸的是,這些變量設置爲Nothing在互操作的情況下不釋放Excel進程句柄......但是,如果你明確地釋放每個練成COM對象引用您已經實例化,一旦excel被用戶關閉,該進程不應該被掛起。當我跑到下面的代碼,並關閉Excel時,過程不再掛(我離開了橙因爲它仍設置爲無,並釋放一個對象,它是沒有拋出異常):

Public Class Form1 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Dim oExcel As Excel.Application = Nothing 
     Dim oWorkbook As Excel.Workbook = Nothing 
     Dim oWorksheet As Excel.Worksheet = Nothing 

     oExcel = CreateObject("Excel.Application") 
     oExcel.DisplayAlerts = False 
     oExcel.Visible = False 

     oWorkbook = oExcel.Workbooks.Add 
     oWorksheet = oWorkbook.ActiveSheet 
     'Populate, format, etc. 
     'oWorkbook.SaveAs(Me.txtExportLocation.Text & "\sales.xlsx") 

     oExcel.Visible = True 

     System.Runtime.InteropServices.Marshal.ReleaseComObject(oWorksheet) 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(oWorkbook) 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel) 
     Me.Close() 
    End Sub 
End Class 
+0

更新我的OP與一些新的信息 – Tom

+1

我把你編輯的代碼放到一個程序中並運行它(沒有co的sql並且我無法重現這個問題,只要我關閉了excel,excel進程就會關閉。我不知道爲什麼他們掛在你的電腦上... – soohoonigan

+0

感謝您的嘗試,我不知道爲什麼我遇到這種行爲。希望別人可以參加。 – Tom

2

您需要使用它後處置和釋放excel對象。

一個簡單的代碼,我爲了用它來關閉Excel,即使在任務管理器:

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

它適用於我的應用程序有關調用像`oExcel.quit

+0

用一些新信息更新了我的OP – Tom