Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I am running following code Excel object remains in task manager.

Excel.Application appExl = new Excel.Application();
Excel.Workbook workbook = null;
Excel.Worksheet nwSheet = null;
Excel.Range last = null, range = null;

//Open Excel

workbook = appExl.Workbooks.Open(@txtBrowse.Text, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

nwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);

last = nwSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
range = nwSheet.get_Range("A1", last);

int lastUsedRow = last.Row;
nwSheet.Cells[1, 1].Value2 = "test";
workbook.Save();
C#
if (last != null)
                    Marshal.FinalReleaseComObject(last);
                last = null;
                if (range != null)
                    Marshal.FinalReleaseComObject(range);
                range = null;
                if (nwSheet != null)
                    Marshal.FinalReleaseComObject(nwSheet);
                nwSheet = null;
                if (workbook != null)
                {
                    workbook.Close(true, Missing.Value, Missing.Value);
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workbook);
                }
                workbook = null;
                if (appExl != null)
                {
                    ((Excel._Application)appExl).Quit();
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(appExl);
                }
                appExl = null;


GC.Collect();


I have tried this one also

workbook.Close();
Marshal.ReleaseComObject(workbook);
appExl.Quit();
Marshal.ReleaseComObject(appExl);
workbook=null;
appExl=null;
GC.Collect();
Posted

1 solution

get the list of running processes and kill the one you are using.
 
Share this answer
 
Comments
Prasannala 27-Nov-13 1:44am    
Thank you.Killing excel process is working for me.
But I read one post that killing process is not good way to close excel obj.
agent_kruger 27-Nov-13 5:25am    
no killing is just closing the excel object and if you found this answer useful accept the solution.

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