2016-12-09 68 views
0

问题:为什么在程序运行时Excel进程不关闭?请不要跳枪,并标记重复。如果您可以在代码中显示所需的更改,我真的很感激它。 当我关闭应用程序时,Excel进程关闭很好。我在最近几天研究了这个问题,阅读了几篇SO帖子,并尝试了几件事情,但除了调用process.kill外,没有什么可行的,我宁愿尽可能避免。为什么在程序运行时Excel进程关闭

Imports Microsoft.Office.Interop 
Imports System.Runtime.InteropServices 

Public Class Form1 

    Public Sub New() 

     ' This call is required by the designer. 
     InitializeComponent() 

     ' Add any initialization after the InitializeComponent() call. 
    End Sub 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Dim xlApp As Excel.Application 
     Dim xlWorkBook As Excel.Workbook 
     Dim misValue As Object 
     Dim xlWorkSheet As Excel.Worksheet 

     Try 
      ''EXCEL CREATION/INITAILIAZATION 
      misValue = System.Reflection.Missing.Value 
      xlApp = New Microsoft.Office.Interop.Excel.Application() 
      If xlApp Is Nothing Then 
       MessageBox.Show("Excel is not properly installed!!") 
       xlApp = Nothing 
      End If 
      xlWorkBook = xlApp.Workbooks.Add(misValue) 


      ''WRITE TO WORKSHEET 
      xlWorkSheet = TryCast(xlWorkBook.Sheets("sheet1"), Excel.Worksheet) 
      xlWorkSheet.Cells(1, 1) = "THIS" 
      xlWorkSheet.Cells(1, 2) = "IS" 
      xlWorkSheet.Cells(1, 3) = "A" 
      xlWorkSheet.Cells(1, 4) = "TEST" 

      ''FORCEFULLY CAUSING ERROR, NOW THE EXCEL PROCESS HANGING IN TASK MANAGER 
      ''xlWorkSheet.Cells(1, -1) = "ERROR LINE" 

      ''SAVE WORKSHEET 
      Dim Name = DateTime.Now.ToString("s").Replace(":", "_") 
      Dim Dir = AppDomain.CurrentDomain.BaseDirectory & "Output\" & Name & "Output.xls" 
      xlApp.DisplayAlerts = False 
      xlWorkBook.CheckCompatibility = False 
      xlWorkBook.DoNotPromptForConvert = True 
      xlWorkBook.SaveAs(Dir, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _ 
           Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing) 
      xlWorkBook.Close(False) 
      xlApp.Quit() 

      misValue = Nothing 

      If Not IsNothing(xlWorkSheet) And System.Runtime.InteropServices.Marshal.IsComObject(xlWorkSheet) Then 
       System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet) 
       xlWorkSheet = Nothing 
      End If 

      If Not IsNothing(xlWorkBook) And System.Runtime.InteropServices.Marshal.IsComObject(xlWorkBook) Then 
       System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook) 
       xlWorkBook = Nothing 
      End If 

      If Not IsNothing(xlApp) And System.Runtime.InteropServices.Marshal.IsComObject(xlApp) Then 
       System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp) 
       xlApp = Nothing 
      End If 

      GC.Collect() 
      GC.WaitForPendingFinalizers() 

     Catch ex As Exception 
      Dim exMsg = ex.Message 
     End Try 
    End Sub 

End Class 
+0

这不是一个答案,但如果可以的话,我建议你找一个好的API来保存excel文件。通过interop你可以得到这样的问题,而且速度很慢。 –

+0

你的问题是什么?你清楚大胆* Excel过程关闭很好,当我关闭应用程序*反对你的问题。另外,对VB.Net不太熟悉,但是你应该在'Catch'中释放资源('xlApp = Nothing'),或者在['Finally']中释放资源(https://msdn.microsoft.com/zh-cn/ -us/library/fk6t46tz.aspx)条款 – Parfait

+0

@Parfait问题是第一个大胆的陈述。如果对象是在sub中声明的,那么在sub的末尾它应该超出范围。为了使这个更清楚,如果你运行上面的代码并点击几次按钮,你会注意到所有的excel流程实例,除了一个会在子结尾处关闭。 – glant

回答

0

您的问题是.net中的双点引用。在这里阅读:https://msdn.microsoft.com/en-us/library/8bwh56xe(v=vs.110).aspx

所以一旦你解构

xlWorkBook = xlApp.Workbooks.Add(misValue) 

xlWorkBooks = xlApp.Workbooks 
xlWorkBook = xlWorkBooks.Add(misValue) 

然后松开,你都好。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Call doSomeWork() 
    GC.Collect() 
End Sub 

Private Sub doSomeWork() 
    Dim xlApp As Excel.Application 
    Dim xlWorkBook As Excel.Workbook 
    Dim xlWorkBooks As Excel.Workbooks '// Added new variable to avoid double dot. 
    Dim misValue As Object 
    Dim xlWorkSheet As Excel.Worksheet 

    Try 
     ''EXCEL CREATION/INITAILIAZATION 
     misValue = System.Reflection.Missing.Value 
     xlApp = New Microsoft.Office.Interop.Excel.Application() 
     If xlApp Is Nothing Then 
      MessageBox.Show("Excel is not properly installed!!") 
      xlApp = Nothing 
     End If 
     xlWorkBooks = xlApp.Workbooks 
     xlWorkBook = xlWorkBooks.Add(misValue) 


     ''WRITE TO WORKSHEET 
     xlWorkSheet = xlWorkBook.Sheets(1) 

     'xlWorkSheet = TryCast(sheets("sheet1"), Excel.Worksheet) 
     xlWorkSheet.Cells(1, 1) = "THIS" 
     xlWorkSheet.Cells(1, 2) = "IS" 
     xlWorkSheet.Cells(1, 3) = "A" 
     xlWorkSheet.Cells(1, 4) = "TEST" 

     ''FORCEFULLY CAUSING ERROR, NOW THE EXCEL PROCESS HANGING IN TASK MANAGER 
     ''xlWorkSheet.Cells(1, -1) = "ERROR LINE" 

     ''SAVE WORKSHEET 
     Dim Name = DateTime.Now.ToString("s").Replace(":", "_") 
     Dim Dir = AppDomain.CurrentDomain.BaseDirectory & "Output\" & Name & "Output.xls" 
     xlApp.DisplayAlerts = False 
     xlWorkBook.CheckCompatibility = False 
     xlWorkBook.DoNotPromptForConvert = True 
     xlWorkBook.SaveAs("C:\temp\a.xls", Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _ 
          Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing) 
     xlWorkBook.Close(False) 
     xlApp.Quit() 

     misValue = Nothing 

    Catch ex As Exception 
    End Try 
End Sub 
+0

感谢您的回答。我已经尝试过使用xlWorkbooks作为单独的变量来避免链接点,但这一切都没有奏效,所以当我看到你的答案奏效时,我真的必须弄清楚为什么它以前不适用于我,并且去了我之间的所有三角洲之间解决方案和你的。原来TRY/CATCH块是罪魁祸首。注释了try/catch块和我上面的原始代码的作品。感谢您的博文给我的动力。 – glant

+0

如果你想添加try/catch块到这个解决方案,它将会失败..至少它为我做了并且在同事盒子上测试过......不知道为什么会发生这种情况 – glant

+0

hmm ....从事件处理程序调用一个单独的子完成所有工作,将允许你使用try catch阻塞。调用该子然后清理。更新后的代码适用于我。 – cyboashu

相关问题