小弟在学习VB2010和SQL2008,想编一个小程序导出EXCEL,但是导出后,系统进程一直有EXCEL的进程去不掉,求各位大师指导
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
[解决办法]
按照这个顺序执行:
wbook .Save
wbook .Close
wapp .Quit
Set wsheet = Nothing
Set wbook = Nothing
Set wapp = Nothing