Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 28th, 2007, 12:59 PM
Registered User
 
Join Date: Mar 2007
Location: , , .
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! 
}
Reply With Quote
  #2 (permalink)  
Old March 29th, 2007, 01:45 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old March 29th, 2007, 06:36 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
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.

Reply With Quote
  #4 (permalink)  
Old March 29th, 2007, 11:19 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:05 AM.


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