Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Excel Application Process in C# after Data Access

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.

enter image description here

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.

like image 560
Javad Yousefi Avatar asked Jul 21 '13 22:07

Javad Yousefi


People also ask

How do you end a process in Excel?

Click Start>Run> type “cmd” and hit enter or click 'OK'. You will notice that all Winword.exe processes are terminated now.

How do you close an Excel File quickly?

The quickest way to close all open workbooks is to hold the Shift key while pressing the Close Window button.


1 Answers

Try this:

excelBook.Close(0);  excelApp.Quit(); 

When closing the work-book, you have three optional parameters:

Workbook.close SaveChanges, filename, routeworkbook  

Workbook.Close(false) or if you are doing late binding, it sometimes is easier to use zero Workbook.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

like image 182
Michael Avatar answered Sep 25 '22 19:09

Michael