Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 March 28th, 2007, 12:59 PM
Registered User
 
Join Date: Mar 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel Application is left in Memory, why???

I would like to open the excel workbook and then change some formats and save back with the new file name.

When I use the following code, if there is no error, it's OK. Excel object is swept from the memory.
But if there are some errors, Excel Object is still the memory. Even though I use try..catch..finally block, it still happens. Help Me.

Thanks,
TW

Code:
public Excel.Application oXL        = null;
public Excel._Workbook oWB        = null;
public Excel.Sheets excelSheets        = null;    

try 
{
    oXL = new Excel.ApplicationClass();        // Run the Excel Application

    string workbookPath = txtfile.Text;        // Get the Filename

    // Opening the Excel Workbook
    oWB = oXL.Workbooks.Open(workbookPath,
        0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
        true, true, 0, true, false, false);

    excelSheets = oWB.Worksheets;

    // Changing the format of the Excel
    foreach (Excel.Worksheet worksheet in excelSheets)
    {
        //Change the format of the Sheet.....

    }            

    // Output Filename
    string newFileName    = "testing.xls";

    // Saving the formatted Excel
    oWB.SaveAs( newFileName,Excel.XlFileFormat.xlWorkbookNormal, 
            null,null,false,false,Excel.XlSaveAsAccessMode.xlShared,
            false,false,null,null,null);                                

    // Show Success Message
    MessageBox.Show("Your file is successfully changed!","SUCCESS",MessageBoxButtons.OK,MessageBoxIcon.Information);        

}
catch (Exception exc)
{                
    MessageBox.Show(exc.Message,"ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally 
{
    // Need all following code to clean up and extingush all references!!!
    oWB.Close(false,null,null);    
    oXL.Workbooks.Close();
    oXL.Quit();                                

    System.Runtime.InteropServices.Marshal.ReleaseComObject (excelSheets);
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);

    oWB        = null;                
    excelSheets    = null;
    oXL        = null;

    GC.Collect(); // force final cleanup! 
}
 
Old March 29th, 2007, 01:45 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Where do the errors occur and what are they? If they occur within the finally block itself then all the code won't run. You may need to restructure the try/catch/finally to catch this.

--

Joe (Microsoft MVP - XML)
 
Old March 29th, 2007, 06:36 AM
Registered User
 
Join Date: Mar 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The error occurs in Try Section.
That error occurs when there is already a filename with "testing.xls".
It occurs exactly at "SaveAs" Statement.

 
Old March 29th, 2007, 11:19 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I'm not sure, you might try setting oXL.UserControl to true after creating it.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Remove Excel Object From Memory jatinp82 BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 2 May 30th, 2007 03:53 PM
EXCEL process stays in memory even after deallocat kool_creative General .NET 0 January 13th, 2006 05:04 AM
write multi-sheet Excel w/o Excel.Application manmoth Classic ASP Components 2 November 22nd, 2005 10:56 AM
excel export giving memory error msrnivas .NET Web Services 0 September 6th, 2004 12:09 AM





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