0

I am currently struggling with a problem where I am not able to close the EXCEL.EXE process after creating a new Excel file and adding value into the cells. There are plenty of threads out there, in which the solutions work for some people and for others it does not. As I have worked through almost every thread now - without finding a solution, I am trying to give it one more try here.

            Application excelNew = null;
            Workbooks newWbs = null;
            Workbook newWb = null;
            Worksheet newWs = null;
            try
            {
                excelNew = new Application();
                excelNew.DisplayAlerts = false;
                newWbs = excelNew.Workbooks;
                newWb = newWbs.Add();
                newWs = newWb.Worksheets[1];
                newWs.Name = "Compared params";
                int k = 1;
                newWs.Cells[k, 1] = "FILE";
                newWs.Cells[k, 2] = "NAME";
                newWs.Cells[k, 3] = "SEC";
                newWs.Cells[k, 4] = "KEY";
                newWs.Cells[k, 5] = "TEN";
                newWs.Cells[k, 6] = "HOST";
                newWs.Cells[k, 7] = "SOURCE";
                newWs.Cells[k, 8] = "TARGET";
                k++;

                foreach (var c in comparedParamTupleList)
                {
                    newWs.Cells[k, 1] = c.Item1;
                    newWs.Cells[k, 2] = c.Item2;
                    newWs.Cells[k, 3] = c.Item3;
                    newWs.Cells[k, 4] = c.Item4;
                    newWs.Cells[k, 5] = c.Item5;
                    newWs.Cells[k, 6] = c.Item6;
                    newWs.Cells[k, 7] = c.Item7;
                    newWs.Cells[k, 8] = c.Rest.Item1;
                    k++;

                }
                newWb.SaveAs(pathDirectory + "\\Output.xlsx");
               
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                
                if (newWb != null)
                {
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(newWs);
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(newWb);
                }
                if (newWbs != null)
                {
                    newWbs.Close();
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(newWbs);
                }
                if (excelNew != null)
                {
                    excelNew.Quit();
                    //excelNew = null;
                    if (excelNew != null)
                    {
                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelNew) ;
                    }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }```

I really hope that you can support me on this particular topic and enlighten me on why this is not working.
If I would comment out the ".Cells"-part it would close without any issues. I am not able not find the cause though.

Thanks a lot!
Steve
  • 49
  • 1
  • 9
  • 2
    Have you seen my question about this here: https://stackoverflow.com/questions/15697282/application-not-quitting-after-calling-quit Also from what I can see, you need to make sure you close the workbook, you're not here. Flow is typically -> close -> quit (excelNew) and then release the objects. Also my link is about `vb.net`, but it's the same for `c#`. – Trevor Nov 24 '20 at 16:08
  • Hi, I have already tried that. I was also closing everything manually by trying: .Close and then Release. I have also tried setting everything to null manually. – Steve Nov 24 '20 at 16:12
  • 1
    Put the `GC.` stuff *outside* your method (so that the variables are no longer in scope and try it in Release build. I can't find it at the moment, but I remember a post or comment by Hans Passant in which he explains why this works (even without all the "release COM object" magic). – Heinzi Nov 24 '20 at 16:13
  • @Heinzi I remember Hans post about that as well, I'll see if I can dig it up. – Trevor Nov 24 '20 at 16:14
  • 1
    @Codexer: I just found it: https://stackoverflow.com/a/25135685/87698 – Heinzi Nov 24 '20 at 16:15
  • Hi guys, I have seen that already. I tried it with the released build aswell. – Steve Nov 24 '20 at 16:15
  • 2
    @Steve: Just make sure you read Hans post in detail and *really* try to understand it. It's not a copy&paste solution, but once you understand the reasoning behind it, everything should become clear. – Heinzi Nov 24 '20 at 16:16
  • @Heinzi good find, that was quick! – Trevor Nov 24 '20 at 16:16
  • 1
    @Codexer: Yep. Google's algorithms never cease to amaze me (at least, when you already know exactly what you are looking for). – Heinzi Nov 24 '20 at 16:17

0 Answers0