Click here to Skip to main content
15,888,024 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello, here's the scenario:

Im using datagridview to export data to Excel, after many times of exporting, i have noticed that my computer become slow, and check the Processes and i saw that there's too many Excel.exe running. What i did is I closed the Application and it removes on the process.

My Question is, how to properly dispose the Excel, After exporting data, so i wont quit my application everytime. My code below:

C#
Microsoft.Office.Interop.Excel._Application Excel = new Microsoft.Office.Interop.Excel.Application();

                  Workbook wb = Excel.Workbooks.Add(XlSheetType.xlWorksheet);

                  Worksheet ws = (Worksheet)Excel.ActiveSheet;

                  //ws.PageSetup.Zoom = false;

                  //ws.PageSetup.FitToPagesTall = 1;
                  //ws.PageSetup.FitToPagesWide = 1;

                  Excel.Visible = true;

                  ws.Cells.EntireColumn.AutoFit();
                  ws.Cells.EntireRow.AutoFit();
                  ws.Cells.Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; // Align cells to lef (excel)(

Please use my code for the revision . thanks
Posted
Updated 14-Jul-20 2:30am
v2

 
Share this answer
 
Comments
Rencyrence 5-Jan-16 22:20pm    
Thank you it helps me alot!
EXCEL and WORD are very and unmanaged objects, so GC is unable to collect them In EXCEL case first we need to we need to use 'Marshal.ReleaseComObject' method to release COM object or each sheet and then we need to dispose EXCEL Workbook first and then quit to EXCEL application object
see below cleanup group
C#
// Cleanup
GC.Collect();
GC.WaitForPendingFinalizers();

Marshal.FinalReleaseComObject(xlRng);
Marshal.FinalReleaseComObject(xlSheet);

xlBook.Close(Type.Missing, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(xlBook);

xlApp.Quit();
Marshal.FinalReleaseComObject(xlApp);
 
Share this answer
 
Since you have used Excel.Visible = true, excel will be visible to the user on screen. So what to do with the excel on screen is up to the user. User can either save it and close it or just discard it without saving. And when the user closes the excel the program, the process also should be closed/terminated.
 
Share this answer
 
Above all solution are correct only one thing missing, still in the background task excel empty object still run for remove that you can put below code at last:

Process[] excelProcesses = Process.GetProcessesByName("excel");
        foreach (Process p in excelProcesses)
        {
            if (string.IsNullOrEmpty(p.MainWindowTitle)) // use MainWindowTitle to distinguish this excel process with other excel processes 
            {
                p.Kill();
            }
        }
 
Share this answer
 
Comments
CHill60 14-Jul-20 8:50am    
Not so, if done properly. However, if you are going to copy solutions from other sites it's nice to credit them c# - How can I dispose my Excel Application - Stack Overflow[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900