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.
Click Start>Run> type “cmd” and hit enter or click 'OK'. You will notice that all Winword.exe processes are terminated now.
The quickest way to close all open workbooks is to hold the Shift key while pressing the Close Window button.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With