Excel process remains open after interop; traditional method not working

后端 未结 4 1440
鱼传尺愫
鱼传尺愫 2021-01-05 02:39

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 exi

4条回答
  •  孤城傲影
    2021-01-05 03:01

    This has worked successfully for me:

            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();
    

提交回复
热议问题