I'm writing an application in C# that opens an Excel template file for read/write operations. I want to when user closes the application, excel application process has been closed, without saving excel file. See my Task Manager after multiple runs of the app.
I use this code to open the excel file :
public Excel.Application excelApp = new Excel.Application();
public Excel.Workbook excelBook;
excelBook = excelApp.Workbooks.Add(@"C:/pape.xltx");
and for data access I use this code :
Excel.Worksheet excelSheet = (Worksheet)(excelBook.Worksheets[1]);
excelSheet.DisplayRightToLeft = true;
Range rng;
rng = excelSheet.get_Range("C2");
rng.Value2 = txtName.Text;
I see similar questions in stackoverflow such as this question and this, and test answers, but it doesn't works.
Ref: https://stackoverflow.com/a/17367570/132599
This resolved the issue for me. Your code becomes:
And then release all those objects:
I wrap this in a
try {} finally {}
to ensure everything gets released even if something goes wrong (what could possibly go wrong?) e.g.Think of this, it kills the process:
Also, did you try just close it normally?
Use a variable for each Excel object and must loop
Marshal.ReleaseComObject >0
. Without the loop, Excel process still remain active.Based on another solutions. I have use this:
Using the "MainWindowHandle" to identify the process and close him.
excelObj: This is my Application Interop excel objecto
I met the same problems and tried many methods to solve it but doesn't work. Finally , I found the by my way. Some reference enter link description here
Hope my code can help someone future. I have been spent more than two days to solve it. Below is my Code:
Most of the methods works, but the excel process always stay until close the appliation.
When kill excel process once it can't be executed once again in the same thread - don't know why.