Excel进程仍互操作开放后;传统的方法无法正常工作无法正常、进程、传统、操作

由网友(塔里以南,木河以北)分享简介:我运行到一个问题,一些code我调试。 Excel的互操作是用来提取一个工作簿中的一些值;但是,Excel保持打开的程序退出后。我已经试过了传统的解决方案,但它仍然有一个参考到Excel在那里的code运行时所有的机器打开I'm running into an issue with some code I'm deb...

我运行到一个问题,一些code我调试。 Excel的互操作是用来提取一个工作簿中的一些值;但是,Excel保持打开的程序退出后。我已经试过了传统的解决方案,但它仍然有一个参考到Excel在那里的code运行时所有的机器打开

I'm running into an issue with some code I'm debugging. Excel interop is used to extract some values from a workbook; however, Excel remains open after the program has exited. I've tried the traditional solution, but it still keeps a reference to Excel open on all machines where the code is run

private void TestExcel()
    {
        Excel.Application excel = new Excel.Application();
        Excel.Workbooks books = excel.Workbooks;
        Excel.Workbook book = books.Open("C:test.xlsm");

        book.Close();
        books.Close();
        excel.Quit();

        Marshal.ReleaseComObject(book);
        Marshal.ReleaseComObject(books);
        Marshal.ReleaseComObject(excel);
    }

即使这样简单的一张code保持与多个文件(XLSM,XLSX,XLS)运行的进程。现在,我们已经制定杀死我们已经打开了的Excel进程的解决方法,但我宁愿得到这个工作对我自己的理智。

Even this simple piece of code keeps the process running with multiple files (xlsm, xlsx, xls). Right now we have a workaround in place to kill the Excel processes we've opened, but I'd much rather get this working for my own sanity.

我要补充一点,我把它缩小到工作簿变量。如果我删除调用 books.Open()和所有的参照,以则成功关闭。

I should add that I have it narrowed down to the Workbook variable. If I remove the call to books.Open() and all references to book then it closes successfully.

推荐答案

这对我来说成功运行:

        xlApp.Quit();

        //release all memory - stop EXCEL.exe from hanging around.
        if (xlWorkBook != null) { Marshal.ReleaseComObject(xlWorkBook); } //release each workbook like this
        if (xlWorkSheet != null) { Marshal.ReleaseComObject(xlWorkSheet); } //release each worksheet like this
        if (xlApp != null) { Marshal.ReleaseComObject(xlApp); } //release the Excel application
        xlWorkBook = null; //set each memory reference to null.
        xlWorkSheet = null;
        xlApp = null;
        GC.Collect();
阅读全文

相关推荐

最新文章