Click here to Skip to main content
11,923,046 members (60,524 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


11 bookmarked

Releasing Excel after using Interop

, 23 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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.WriteLine("After cleanup.");

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:

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:

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

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


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


About the Author

Igor Merabishvili
Software Developer (Senior)
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

AnswerThank you..... Pin
aravinth santosh12-Nov-14 6:54
memberaravinth santosh12-Nov-14 6:54 
QuestionAlternative for Marshal.ReleaseComObject(); Pin
Suraj S Koneri6-Nov-12 22:24
memberSuraj S Koneri6-Nov-12 22:24 
GeneralImplicit COM instance is created [modified] Pin
wmjordan27-Mar-11 18:55
memberwmjordan27-Mar-11 18:55 
GeneralRe: Implicit COM instance is created Pin
Igor Merabishvili7-Apr-11 0:48
memberIgor Merabishvili7-Apr-11 0: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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.3 | Last Updated 23 Mar 2011
Article Copyright 2011 by Igor Merabishvili
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid