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.
You may kill process with your own
COM
object excel pidadd somewhere below dll import code
and use
this GetWindowThreadProcessId finds the correct Process Id o excell .... After kills it.... Enjoy It!!!
Try this:
When closing the work-book, you have three optional parameters:
Workbook.Close(false)
or if you are doing late binding, it sometimes is easier to use zeroWorkbook.Close(0)
That is how I've done it when automating closing of workbooks.Also I went and looked up the documentation for it, and found it here: Excel Workbook Close
Thanks,
It Closes last 10 sec process with name "Excel"
We can close the Excel Application while converting xls to xlsx by using following code. When we perform this kind of task then Excel application is running in task manager, we Should close this excel which is running in background. Interop is a Com component ,to release the com component we used Marshal.FinalReleaseComObject.
try this.. it worked for me... you should release that xl application object to stop the process.