Closing Excel Application Process in C# after Data Access

Javad Yousefi picture Javad Yousefi · Jul 22, 2013 · Viewed 158k times · Source

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.

Answer

Michael picture Michael · Jul 22, 2013

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

Thanks,