Closing Excel Application Process in C# after Data Access
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 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
Closing Excel process after creating using Excel COM
Try this approach…
Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook = null;
try {
// code that works with excel…
}
catch (Exception ex) {
MessageBox.Show("Excel Error " + ex.Message);
}
finally {
if (xlWorkBook != null) {
xlWorkBook.Close();
Marshal.ReleaseComObject(xlWorkBook);
}
if (xlApp != null) {
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}
//GC.Collect();
}
Edit… per OP comment…
I am guessing that you may be misinterpreting what the task manager is posting. For starters, .NET uses a Garbage Collector to manage its unused memory. And if you want to “immediately” get rid of the multiplying instances of Excel that appear to be lingering in the Task Manger is to call the .NET Garbage Collector directly like…
GC.Collect();
HOWEVER,… I DO NOT RECOMMEND THIS.
If you exit the application, then ALL the instances will get released. If the app continues to run, then usually at least one instance of Excel will remain. However, when the application is running... Often the Garbage Collector will not release this memory from the app if it has plenty of memory already, which makes sense.
So, even though it still appears in the task manager, if push comes to shove and something NEEDS memory, it will release it. Which is the case here. Granted there may be many instances of Excel running… if you run the code many times over without exiting the application… the Excel apps will "eventually" get released. In my tests, usually after about 10-20 apps were running, they would all get released and it would go down to one or two Excel instances. Then it would grow again. This is the job of the Garbage Collector.
Calling the Garbage Collector's Collect
code above will release the app “immediately” as you want, however, I would say be careful. It is not recommended that you do this. So, I say pick your own poison here.
Close EXCEL application process after data has been fetched
I've been there before. Here's an article that really helped me sort it out:
http://devcity.net/Articles/239/1/article.aspx
Excel seems to be very stubborn about terminating the process. You will more than likely end up killing the process using System.Diagnostics.Process.
Close EXCEL application process after data has been fetched
I've been there before. Here's an article that really helped me sort it out:
http://devcity.net/Articles/239/1/article.aspx
Excel seems to be very stubborn about terminating the process. You will more than likely end up killing the process using System.Diagnostics.Process.
How to close excel in C#?
This is something I've played around with a lot while using SSIS Script Tasks to refresh Excel files.
I've read mixed things about using Marshal.ReleaseComObject
, but I've also found that it isn't necessary. For me, the ultimate solution was found to be the following:
using xl = Microsoft.Office.Interop.Excel;
...
public void Main()
{
DoExcelWork();
GC.Collect();
GC.WaitForPendingFinalizers();
}
private void DoExcelWork()
{
xl.Application app = null;
xl.Workbooks books = null;
xl.Workbook book = null;
try
{
app = new xl.Application() { DisplayAlerts = false };
books = app.Workbooks;
book = books.Open("file path goes here");
book.RefreshAll();
// this is where you would do your Excel work
app.DisplayAlerts = false; // This is for reinforcement; the setting has been known to reset itself after a period of time has passed.
book.SaveAs("save path goes here");
app.DisplayAlerts = true;
book.Close();
app.Quit();
}
catch
{
if (book != null) book.Close(SaveChanges: false);
if (app != null) app.Quit();
}
}
I'm not sure how your application is laid out, but when using SSIS I found it was necessary to call GC.Collect
outside of the scope where the Excel Interop objects were declared in order to avoid having the Excel instances left open on some occasions, hence the two methods.
You should be able to adapt this code to suit your requirements.
Cannot close Excel.exe after Interop process
Simple rule: avoid using double-dot-calling expressions, such as this:
var workbook = excel.Workbooks.Open(/*params*/)
...because in this way you create RCW objects not only for workbook
, but for Workbooks
, and you should release it too (which is not possible if a reference to the object is not maintained).
So, the right way will be:
var workbooks = excel.Workbooks;
var workbook = workbooks.Open(/*params*/)
//business logic here
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(excel);
Related Topics
Difference Between Casting and Using the Convert.To() Method
Applying CSS on ASP.NET Fileupload Control's Browse Button Only
A Method for Making Http Requests on Unity iOS
Web Browser Control: How to Capture Document Events
Posting JSON to Url via Webclient in C#
Using Reflection in C# to Get Properties of a Nested Object
How to Manipulate Images at the Pixel Level in C#
C# - Winforms - Global Variables
How to Find Multiple Occurrences with Regex Groups
Implicit VS Explicit Interface Implementation
Cross Platform (PHP to C# .Net) Encryption/Decryption with Rijndael
Style Bundling Not Working After Iis Deployment (MVC 4)
How to Add a Custom View to a Xib File Defined View in Monotouch
Request Exceeds the Configured Maxquerystringlength When Using [Authorize]