Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
4.00/5 (1 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
Updated 2-Jan-13 9:13am
v2

1 solution

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);
C#
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900