vb.net – 关闭Excel.exe进程

前端之家收集整理的这篇文章主要介绍了vb.net – 关闭Excel.exe进程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面的代码工作,但excel.exe进程仍然运行,即使我退出Excel.我正在使用Office 2013并引用Office.Interop.Excel的正确导入

我错过了什么

Sub demo()
    Dim xls As New Excel.Application
    Dim book As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    xls.Workbooks.Open("Test.xlsx")
    book = xls.ActiveWorkbook
    oSheet = book.ActiveSheet   

    oSheet.Cells(1,2).Value = "testing"

    book.Save()
    book.Close()
    xls.Workbooks.Close()
    xls.Quit()

    System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(book)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xls)
    oSheet = Nothing
    book = Nothing
    xls = Nothing
    GC.Collect()
End Sub

解决方法

如果你仍然遇到问题,你试图杀死这个过程吗?

Process[] procs = Process.GetProcessesByName("name");
foreach (Process proc in procs)
    proc.Kill();

不知道它是否会按照你想要的方式工作,但这是一个想法.

猜你在找的VB相关文章