0

Need help on getting a simple method to readxml. Here are the steps that I have taken:

  1. Added reference from Microsoft Excel 12.0 Object Library COM.
  2. Copied code exactly from another source http://csharp.net-informations.com/excel/csharp-read-excel.htm
  3. The only difference that I would require this to be a method and Not an onclick event.
  4. The error I'm getting is Error 10 An object reference is required for the non-static field, method, or property on code releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp);

What are the steps required?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace projectName
{
class frmReadXml
{

    public static void executeRead()
    {

        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet = new Excel.Worksheet();
        Excel.Range range;

        string str;
        int rCnt = 0;
        int cCnt = 0;



        xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        range = xlWorkSheet.UsedRange;

        for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
        {
            for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
            {
                str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                MessageBox.Show(str);
            }
        }

        xlWorkBook.Close(true, null, null);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);
    }
    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    } 

}

}

user2552331
  • 77
  • 1
  • 2
  • 10

2 Answers2

0

Because your function:

public static void executeRead()

is declared as static, it only has access to other functions that are also defined as static. Either remove the static on this function, or add it to the others. Since none of these seem to access any class members of the frmReadExcel, I suggest you make them all static.

Michael Bray
  • 14,998
  • 7
  • 42
  • 68
  • Thanks.. I'll just remove static since I have no clue what it does exponentially. – user2552331 Jul 05 '13 at 04:49
  • Perhaps this article will help you understand what `static` means: http://stackoverflow.com/questions/4124102/whats-a-static-method-in-c – Michael Bray Jul 05 '13 at 04:51
  • ah turns out i would require to use static for this to be accessed from another form. I'll just add static to the other methods then. – user2552331 Jul 05 '13 at 05:00
  • Perhaps, yes... but realize that this static class will only have ONE and ONLY ONE version of it throughout your entire application. That is, if you need to use this class more than once in your application, you might find that the values change when you don't expect them to. Think of a "static" as meaning "one copy that is kept for the entire lifetime of the application". – Michael Bray Jul 05 '13 at 05:05
  • Then this would be a huge problem if the values will be changed in the function. How should I approach this then? using inheritance? Not much understanding of the SRP but was instructed to do in such a way. – user2552331 Jul 05 '13 at 05:13
  • If you think you will use this class more than once, or at the same time in different parts of your app, then don't make the functions static, and then create an instance of the class: `frmReadXml frx = new frmReadXml(); frx.executeRead();` – Michael Bray Jul 05 '13 at 05:19
0

make releaseObject as static method

private static void releaseObject(object obj)
Damith
  • 62,401
  • 13
  • 102
  • 153