Click here to Skip to main content
15,903,362 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Tip/Trick

Releasing Excel after using Interop

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
23 Mar 2011CPOL 40.2K   11   4
A small tip how to release Excel object after using Interop

Some time ago, I wrote a simple application to fill in and write on disk an Excel spreadsheet. Something like this:


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Net;
using System.IO;

namespace ExcelInterop
{
    class Program
    {
        static void Main(string[] args)
        {
            Application oXL = null;
            _Workbook oWB = null;
            Workbooks oWBs = null;
            _Worksheet oSheet = null;
            Range oRange = null;

            oXL = new Application();
            oXL.Visible = false;
            oXL.DisplayAlerts = false;

            oWBs = oXL.Workbooks;

            oWB = oWBs.Add(1);

            oSheet = oWB.ActiveSheet;

            oRange = oSheet.Cells[1,1];
            oRange.Value = "Test";

            oWB.SaveAs(@"C:\Temp\Test.xls", XlFileFormat.xlWorkbookNormal, 
                       null, null, false, false, XlSaveAsAccessMode.xlShared, 
                       false, false, null, null, null);

            Console.WriteLine("Before cleanup.");
            Console.ReadKey(false);

            oWB.Close();
            oXL.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(oRange);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oWBs);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);

            Console.WriteLine("After cleanup.");
            Console.ReadKey(false);
        }
    }
}

Although the program worked as expected, I encountered an annoying problem. The Excel application kept running even after releasing all Interop objects. After some investigation, I realized that the problem was caused by the following two lines of code:


C#
oRange = oSheet.Cells[1,1];
oRange.Value = "Test"

If I comment them out, then after the cleanup, the Excel application will disappear from the list of running processes. But obviously, I needed to set the right value into the cell. So after some Googling, I ended up with the following alternative:


C#
oRange = oSheet.Cells;
oRange.set_Item(1, 1, "Test");

This solution works perfectly, and releases the Excel application after clean up.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerThank you..... Pin
aravinth santosh12-Nov-14 5:54
aravinth santosh12-Nov-14 5:54 
QuestionAlternative for Marshal.ReleaseComObject(); Pin
Suraj S Koneri6-Nov-12 21:24
Suraj S Koneri6-Nov-12 21:24 
GeneralImplicit COM instance is created [modified] Pin
wmjordan27-Mar-11 17:55
professionalwmjordan27-Mar-11 17:55 
oRange = oSheet.Cells[1,1];


Actually you have created two COM instances here. One is oSheet.Cells, and the other is the cell object returned by the item method of the oSheet.Cells object.
So, instead of the set_item method, you can use the following alternative:
oCells = oSheet.Cells;
oCell = oCells[1,1];

Don't forget to release both oCells and oCell.

I've also written a tip above this issue one month ago, and the "chained property access" issue is discussed at the end.

http://www.codeproject.com/Tips/162691/Proper-Way-of-Releasing-COM-Objects-in-NET.aspx

modified on Monday, March 28, 2011 12:01 AM

GeneralRe: Implicit COM instance is created Pin
Igor Merabishvili6-Apr-11 23:48
Igor Merabishvili6-Apr-11 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.