1.

vb.net

Imports Microsoft.Office.Interop
Private Sub NAR(ByVal o As Object)
    Try
      System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
    Catch
    Finally
      o 
= Nothing
    End Try
  End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim oApp As New Excel.Application()
    Dim oBooks As Excel.Workbooks 
= oApp.Workbooks
    Dim oBook As Excel.Workbook 
= oBooks.Add
    Dim oSheet As Excel.Worksheet 
= oApp.ActiveSheet

    NAR(oSheet)
    oBook.Close(False)
    NAR(oBook)
    NAR(oBooks)
    oApp.Quit()
    NAR(oApp)

    GC.Collect()

    Debug.WriteLine(
"Sleeping使用 Visual Studio .NET 客户端执行自动化功能后不退出 Office 应用程序 代码")
    System.Threading.Thread.Sleep(
5000)
    Debug.WriteLine(
"End Excel")
End Sub

 

C#

 o)
{
    try 
    {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
    }
    
catch {}
    
finally 
    {
        o 
= null;
    }
}

   exBook.Close(DBNull.Value, DBNull.Value, DBNull.Value)
            exApp.Workbooks.Close()
            exApp.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(exApp)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(exSheet)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(exBook)
            exApp = Nothing
            exBook = Nothing
            exSheet = Nothing
            GC.Collect()

 参考文章:http://support.microsoft.com/?id=317109

相关文章:

  • 2021-09-21
  • 2022-01-30
  • 2021-12-21
  • 2021-07-07
  • 2022-12-23
  • 2021-11-24
猜你喜欢
  • 2021-07-27
  • 2021-12-22
  • 2021-07-12
  • 2021-09-05
  • 2021-06-05
  • 2021-09-08
  • 2022-12-23
相关资源
相似解决方案