Click here to Skip to main content
15,885,278 members

How to Read Office 2003 exce in C#.Net using Microsoft.Office.Interop.dll 11.0 version dll

LakshmiSimham asked:

Open original thread
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]
Tags: .NET (.NET4), Microsoft Excel, Operations

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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