Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        daochu(DataGridView1)
    End Sub    Private Sub daochu(ByVal dataGridView As DataGridView)
        Dim wapp As New Microsoft.Office.Interop.Excel.Application
        Dim wsheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim wbook As Microsoft.Office.Interop.Excel.Workbook
        On Error Resume Next
        wapp.Visible = True
        wbook = wapp.Workbooks.Add()
        wsheet = wbook.ActiveSheet
        Dim iX As Integer
        Dim iY As Integer
        Dim iC As Integer
        For iC = 0 To dataGridView.Columns.Count - 1
            wsheet.Cells(1, iC + 1).Value = dataGridView.Columns(iC).HeaderText
        Next
        For iX = 0 To dataGridView.Rows.Count - 1
            For iY = 0 To dataGridView.Columns.Count - 1
                wsheet.Cells(iX + 2, iY + 1).value = dataGridView(iY, iX).Value.ToString
            Next
            wbook.Close()
            wapp.Quit()
        Next
        wsheet = Nothing
        wbook = Nothing
        wapp = Nothing
    End Sub

解决方案 »

  1.   

    按照这个顺序执行:
    wbook .Save
    wbook .Close
    wapp .Quit
    Set wsheet = Nothing
    Set wbook = Nothing
    Set wapp = Nothing
      

  2.   

    wbook.Close()
    wapp.Quit()
    这2个怎么写到循环里面去了?
      

  3.   

    我用的是VB2010的,貌似在
      wsheet = Nothing
      wbook = Nothing
      wapp = Nothing
    这三个句子前写不出SET 
    那两个写到循环啊,哎,因为找不到原因,我到处试的,才接触的VB,新买的书在自学
      

  4.   

    wbook.Close()
    wapp.save()
    wapp.Quit()
    这些写在循环外面,加上段点,你单步调试一下看看wapp.quit以后你的execl退出了没有
      

  5.   

     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
    我加了这么一段后,进程顺利退出了,谢谢指导
      

  6.   

     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
    我加了这么一段后,进程顺利退出了,谢谢指导
      

  7.   

    暴力一些的:
    Shell "taskkill /IM EXCEL.EXE",vbHide