vb.net createobject("Excel.Application") 出错
我用vb.net控制excel的代码如下。
For Each node_father In TreeView1.Nodes
board = node_father.FullPath
frmpro.Label1.Text = "Generating Report of " & node_father.Text & ", Please Wait..."
xlExcel = CreateObject("Excel.Application")
'Dim xlExcel As New Microsoft.Office.Interop.Excel.Application
xlExcel.Visible = False
For Each AddIn In xlExcel.AddIns
If AddIn.Installed Then
AddIn.Installed = False
AddIn.Installed = True
End If
Next
Me.Refresh()
frmpro.Refresh()
xlExcel.Run("VssInit")
xlBook = xlExcel.Workbooks.Open(Windows.Forms.Application.StartupPath & "\Report_template\Switchboard_template.xls")
xlSheet = xlBook.Worksheets(1)
xlSheet2 = xlBook.Worksheets(2)
xlSheet.Cells(1, 1).Value = DateTimePicker1.Value
xlSheet.Cells(2, 1).Value = DateTimePicker2.Value
xlSheet.Cells(3, 1).Value = Replace(node_father.Text, "/", "_") & "_Wh_Im"
xlSheet.Cells(4, 3).Value = Replace(node_father.Text, "/", "_")
xlSheet2.Cells(1, 1).Value = DateTimePicker1.Value
xlSheet2.Cells(2, 1).Value = DateTimePicker2.Value
xlSheet2.Cells(3, 1).Value = Replace(node_father.Text, "/", "_") & "_Isys"
xlSheet2.Cells(3, 2).Value = Replace(node_father.Text, "/", "_")
xlExcel.Run("_GenerateReport")
xlExcel.Run("_RemoveAllFormulaNow")
xlExcel.DisplayAlerts = False
xlBook.SaveAs(Windows.Forms.Application.StartupPath & "\Report_raw\" & Replace(node_father.Text, "/", "_") & ".xls")
xlBook.Close(False)
xlExcel.Run("VssTerminate")
xlSheet = Nothing
xlBook = Nothing
xlExcel = Nothing
Shell("taskkill /im EXCEL.exe /f", vbHide)
Next
我循环读取treeview下面的节点,填入excel里面生成报告。
当第一次CreateObject("Excel.Application")的时候,没有问题
循环第二次的时候,CreateObject("Excel.Application")出错,cannot create activex component.
不知道是不是跟我用Shell("taskkill /im EXCEL.exe /f", vbHide)来强行结束excel进程有关。
但是不用Shell("taskkill /im EXCEL.exe /f", vbHide),进程里面的excel结束不掉。
请问下大家有什么办法,谢谢。