Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How to ensure I don't keep keep creating additional EXCEL.EXE processes each time I call a routine in C# from a loop?

I am developing a C# Windows application using VS2005.
In the following routine which I call from a loop,
it seems that I create a new EXECEL.EXE process each time this routine is called
according to the Windows Task Manager.
Is there a way to ensure that I drop the EXCEL.EXE before I call this routine the next time?

public static void ExcelFormat(string filename, string footnote)
{
            Excel.Application oXL = new Excel.Application();
            oXL.Visible = false;
            oXL.DisplayAlerts = false;

            Excel._Workbook oWB = (Excel._Workbook)oXL.Workbooks.Open(filename, 0, false, 5, Missing.Value, Missing.Value, false, Missing.Value, Missing.Value, false, false, Missing.Value, false, false, false);
            Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
            oSheet.Activate();

            try
            {

                oSheet.get_Range(oSheet.Cells[2, 1], oSheet.Cells[65535, 1]).EntireColumn.NumberFormat = "#,##0";

            }
            finally
            {                
                oXL.Workbooks.Close();
                oXL.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
                oSheet = null;
                oWB = null;
                oXL = null;
            }
}
SOLUTION
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of sognoct
sognoct
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial