Excel process still runs after closing in VB.net

前端 未结 4 584
孤城傲影
孤城傲影 2020-12-19 16:43

My question is basically just how to end the Excel.exe process that runs when using excel. In the application I open and use an excel workbook with a couple sheets, then lea

4条回答
  •  感情败类
    2020-12-19 17:24

    I know this is an old thread, but if anyone comes back to this, you actually have to call every Interop.Excel object you touch in the workbook. If you pull in an instantiated class from Excel into your code, when you're done with it, Marshal.ReleaseComObject. Even every cell. It's crazy, but it's the only way I was able to get the same issue resolved.

    And make darn sure you don't have a fatal exception and leave something unreleased... Excel will stay open.

    Excel.Applicaiton? Marshal.ReleaseComObject.

    Excel.Workbook? Marshal.ReleaseComObject.

    Excel.Workshet? Marshal.ReleaseComObject.

    Excell.Range? Marshal.ReleaseComObject.

    Looping through Rows? Marshal.ReleaseComObject every row and cell you loop through.

    Exce.Style? Marshal.ReleaseComObject... At least this is what I had to do...

    If you plan on using the PIA to access Excel, from the first line of code you write, plan how you're going to release your objects. The best approach I've had is to make sure that I pull an excel value out of the Excel object and load it into my own internal variable. Then imediately call Marshal.ReleaseComObject you accessed. Looping through excel objects via a list in a Application, Workbook, Sheet, ListObject, Table, etc, tends to be the hardest to release. Hence planning is rather critical.

提交回复
热议问题