Click here to Skip to main content
12,351,423 members (34,544 online)
Rate this:
 
Please Sign up or sign in to vote.
Hi,

We are developing one Application in c#.Net.
we are using Office2003 , VS 2010 and 4.0 Framework.
In My Application Excel files reading ,Writing and creating sheets.
we have problems in development.
1. At the time of App Reading Excel file. We are trying to open another file (not reading file) by manually then Automatically opening reading excel file along with manually opening excel file. how to prevent app reading excel file opening. we tried so many ways but we did not find solution.
2. to dispose the excel application object we are using Quit() method. this method closing all the excel objects even manually opened excel files also. But we don't wants close manually opened excel files.

Can any one help us . to resolve this issues. this is very important to us.

this is our one method to read excel file


public List serviceCall()
{
    List returnString = new List();
    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Workbook xlWBook;
    Worksheet xlWSheet;
    Range xlRange;
 
    try
    {
        writer.WriteLine(" Step 1 --- Excel file reading started..........");
        xlWBook = xlApp.Workbooks.Open(@"E:\temp.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlApp.Visible = false;
 
        xlWSheet = (Worksheet)xlWBook.Worksheets.get_Item("sheet one");
        xlRange = xlWSheet.UsedRange;
 
        for (int r = 1; r <= xlRange.Rows.Count; r++)
        {
 
            object value = (xlRange.Cells[r, 3] as Range).Value2;
 
            returnString.Add(value == null ? "" : value.ToString());
        }
 
        releaseObject(xlWSheet);
 
        xlWSheet = null;
 
        releaseObject(xlWBook);
 
        xlWBook = null;
 
        releaseObject(xlApp);
    }
    catch (Exception ex)
    {
    }
    finally
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
 
    return returnString;
}
 
 private void releaseObject(object obj)
 {
     try
     {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
     }
     catch (Exception ex)
     {
        obj = null;
     }
     finally
     {
        GC.Collect();
     }
 }
[Edit - wrapped code block]
Posted 2-Jan-13 7:45am
Edited 2-Jan-13 9:13am
CHill60156.9K
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I think what you are trying to say is that you have temp.xls open in Excel, and you are trying to open the file again without using read only mode? Excel keeps track of what files are open and in what modes. It will allow the file to be opened multiple times in read only mode, but only one can have it open in write mode. The one that gets write access will be the first one that opens it. If you need to have that control, you will need to write code that tracks references for open/closed. You could also keep the application open and wrap the Excel API so that multiple calls can be made to the same application instance (this is what I do).


I don't see where you are actually calling .Quit(). Because we are actually creating a new Excel process every time we call new Microsoft.Office.Interop.Excel.Application(), we have to shut down those processes or they will stack up in memory. This can be an especially big problem when we are setting Visible to false.

Try adding these three lines before you call releaseObject(xlWSheet);
object missing = Type.Missing; // missing can replace of many of the API params
xlWBook.Close(missing, missing, missing); // close the workbook 
xlApp.Quit(); // close Excel
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100