Skip to content

Excel open 3 times and wont close until i use the task manag

Sam Urai

Asked May 10, 2016, 6:00 AM EDT1 answer
Share
LinkedInX
Hi
 
Im pretty new to the C# language. Right now im writing a Programm to calculate different
cable dimensioning. Till now everything just worked fine, but now i realised that when i use the programm it opens me three times the excel process and will only close if i propperly close the programm. otherwise everytime i hit the calculate button it will add 3 more excel process to the allready 3. I'm using excel for all the constants  i need for my calculation. I just cant find out where the Problem is so i thought you might help me.
I know the style of my programming isnt very good even bad:-) but im no programmer and most of the stuff i have in my programm is from the internet, because im an newby and dont know or understand some of that.
So if you could help me finding the error i would be very thankfull. The comments and other stuff is written in german, because thats my nativ language i hope this wont be a problem. I guess the error is because im trying befor i open the excel file to catch all possible exceptions with if and else.
thank you

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Ok i solved the Problem by my self. At first i had to remove some of my open Excel function. Actually i opend the Excel 3 times in the Programm on different positions. so i just cut it together to one (i actually only needed two befor the third one was a mistake:-) ) But still i had the Problem, that the excel operation wouldnt stop by it self and everytime i hit the calculate button one more excel showed up in the Taskmanager. So i searched the internet and found this:
    Open Excel:
    Code
    var ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                    var workbook = ExcelApp.Workbooks.Open(myPath);
                    var worksheet = workbook.ActiveSheet as Worksheet;
    then do stuff in it...
    And closing Excel again:
    Code
    workbook.Save();
                    workbook.Close();
                    ExcelApp.Quit();
                    Marshal.ReleaseComObject(ExcelApp);
                    Marshal.ReleaseComObject(workbook);
    
    
                    workbook = null;
                    worksheet = null;
                    ExcelApp = null;
    
                    GC.GetTotalMemory(false);
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.GetTotalMemory(true);
    with this Method one Excel operation is open as long as the Programm is running, but everytime i hit the calculate button now an excel operation opens but closes right after it is done. Its not perfect but it will work like this:-)
    0