Wrox Programmer Forums
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 30th, 2004, 03:59 AM
Registered User
Join Date: May 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel Unload

Excel.Application oXLApp = null;
            Excel.Worksheet oXLWs = null;
            Excel.Workbook oWB = null;

            int ColNumber = 7;
            string templateFilePath = System.Configuration.ConfigurationSettings.AppSett ings["Report_Template"].ToString();
            filePath = System.Configuration.ConfigurationSettings.AppSett ings["Report_FilePath"].ToString();
            fileName = DateTime.Now.Ticks.ToString()+".xls";

            //Create the Report Files Folder, if the Directory doesn't exist
            if (!Directory.Exists(filePath))

            //Throw exception if template file is not found
                throw new NostroException(ExceptionType.SpecificException,Ex ceptionField.ExcelTemplateNotFound);

            File.Copy(templateFilePath,filePath+@"\"+fileName, true);

            GC.Collect();//Clear Any Excel objects created prev.

            oXLApp = new Excel.ApplicationClass();
            oXLWs = new Excel.WorksheetClass();

            //Don't show the Excel App to the user, when it gets loaded
            oXLApp.Visible = false;
            oXLApp.UserControl = false;

            oWB = oXLApp.Workbooks.Open(filePath+@"\"+fileName, 0,false, 5,"", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,0, true);
            oXLWs = (Excel.Worksheet)(oWB.ActiveSheet);

            //oXLApp.Workbooks.Open(filePath+@"\"+fileName,Type. Missing,Type.Missing,Type.Missing,Type.Missing,Typ e.Missing,Type.Missing,Type.Missing,Type.Missing,T ype.Missing,Type.Missing,Type.Missing,Type.Missing );
            //oXLWs = (Excel.Worksheet)oXLApp.Sheets.get_Item(1);

            if (_reportType == ReportTypeEnum.AgeingSummary)
                //Creating headers
                oXLWs.Cells[ColNumber,1] = "Outstanding duration (in days)";
                oXLWs.Cells[ColNumber,2] = "No of entries for Nostro";
                oXLWs.Cells[ColNumber,3] = "Amount for Nostro entry";
                oXLWs.Cells[ColNumber,4] = "No of entries for Mirror";
                oXLWs.Cells[ColNumber,5] = "Amount for Mirror entry";

                //Populate data from ReportModel Array
                for (int i=0; i<_reportModelArray.Length; i++)//Rows
                    oXLWs.Cells[i+ColNumber+1,1] = _reportModelArray[i].OutstandingDateRange;
                    oXLWs.Cells[i+ColNumber+1,2] = _reportModelArray[i].NostroNoOfEntries.ToString();
                    oXLWs.Cells[i+ColNumber+1,3] = _reportModelArray[i].NostroAmount.ToString();
                    oXLWs.Cells[i+ColNumber+1,4] = _reportModelArray[i].MirrorNoOfEntries.ToString();
                    oXLWs.Cells[i+ColNumber+1,5] = _reportModelArray[i].MirrorAmount.ToString();

            else if (_reportType == ReportTypeEnum.Recon)


            //Set the header text to Bold
            oXLWs.get_Range("A"+ColNumber.ToString(),"Z"+ColNu mber.ToString()).Font.Bold = true;
            oXLWs.get_Range("A"+ColNumber.ToString(),"Z"+ColNu mber.ToString()).VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;

            //Set the filepath to Absolute File path (with the file name that is generated)
            filePath += @"\"+DateTime.Now.Ticks.ToString()+".xls";

            //Save the file
            oXLWs.SaveAs(filePath,Excel.XlFileFormat.xlWorkboo kNormal,Type.Missing,Type.Missing,Type.Missing,Typ e.Missing,Type.Missing,Type.Missing,Type.Missing);

This code does not unload the Excel from the memory, it can be seen in the taskManager. Kindly help me out.

The finally code has ::

// oXLApp.Workbooks.Close();
// oXLApp.Quit();
// System.Runtime.InteropServices.Marshal.ReleaseComO bject(oXLApp);
// System.Runtime.InteropServices.Marshal.ReleaseComO bject(oXLWs);
// System.Runtime.InteropServices.Marshal.ReleaseComO bject(oWB);
// oXLWs = null;
// oWB = null;
// oXLApp = null;
// GC.Collect();// force final cleanup!
Old June 4th, 2004, 04:25 AM
Registered User
Join Date: Jun 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Use System.Diagnostics.Process class and destroy instances of EXCEL

Similar Threads
Thread Thread Starter Forum Replies Last Post
unload crystalActivexreportviewer sen22 VB Components 0 April 27th, 2007 02:01 AM
can i unload assembly ? amirxy VS.NET 2002/2003 4 March 25th, 2006 07:35 AM
Unload the Form dpkbahuguna Beginning VB 6 4 March 21st, 2006 06:50 AM
how to unload the values from sql where sriraj VBScript 1 August 9th, 2004 03:45 AM
Excel 2000 Runtime Does Not Unload MG VS.NET 2002/2003 3 January 6th, 2004 02:47 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.