0

For now just want to be able to access an excel cells value and print that value in my console window.

A few of other ways I have tried:

System.Console.WriteLine(Mysheets.Cells[1,1].Tostring);

Note that the method above, after hitting period after the brackets intellisense only gives me option to equals, gethashcode, get type or two string. I saw some calling .value after the brackets but I am not given option to do so.

using Excel = Microsoft.Office.Interop.Excel;

class Program
{
    private static Excel.Workbook MyBook = null;
    private static Excel.Application MyApp = null;
    private static Excel.Worksheet MySheet = null;
    private static Excel.Range MyRangeOne = null;

    public static void Main()
    {
        string path = @"Z:\New folder\Test1234.xlsx";
        MyApp = new Excel.Application();
        MyApp.Visible = true;
        MyBook = MyApp.Workbooks.Open(path);
        MySheet = (Excel.Worksheet)MyBook.Worksheets[1];
        MyRangeOne = (Excel.Range)MySheet.Cells[1,1];

        try {
            System.Console.WriteLine(MySheet.Name);
            System.Console.WriteLine(MyRangeOne.Value);
        }
        catch(System.Exception err){
            System.Console.WriteLine(err.Message);
        }
    }
}

Have also tried

=Myrangeone.item[1,1].value. Keep getting the following error:

Object reference not set to an instance of an object

Amazing how difficult this is.

7
  • I suspect that MySheet is null, because by calling (Excel.Worksheet)MyBook.Worksheets[1]; you are accessing the second worksheet, which could be missing. I would try putting MySheet = (Excel.Worksheet)MyBook.Worksheets[0];. Apart from this, rather than using Microsoft.Office.Interop.Excel, which means having Office effectively installed on the target pc, I suggest the excellent EPPlus library. Commented May 10, 2018 at 16:16
  • thank you for the library, I will take a look at it over the weekend. Hopefully I can find a good youtube tutorial to help out. Mind boggling that the standard library handles a native Microsoft application so awkwardly. As for the sheet being null, that's why i put the writeline(MySheet.Name) in there. I get a value for that "SHEET1" which is the name of the sheet. Commented May 10, 2018 at 16:42
  • @PSkalka: although it looks like you are accessing a normal C# array with Excel interop you are working with COM objects, these are actually 1-offset based - so Worksheets[1] Is the first worksheet & Cells[1,1] refers to cell A1. Commented May 10, 2018 at 16:45
  • @learnAsWeGo: I am not sure why you have a problem - I have tested the code exactly as presented (other than path) & it prints the contents of cell A1 as expected. The reason why Intellisense does not give you the option of Value (or Value2, can also be used) - is that the object is a COM object that it cannot determine information about - if you cast it first then it will work - adding period after this will work ((Range)Mysheets.Cells[1,1]) Commented May 10, 2018 at 16:54
  • I tried System.Console.WriteLine((Excel.Range)MySheet.Cells[1,1]); and got a blank rather than an error which I suppose is an improvement LOL. Commented May 10, 2018 at 17:10

1 Answer 1

1

I recommend you to use this library for Excel sheet processing EPPlus. Your way of processing Excel files is not very efficient. So please check out it here.

This library can be installed via NuGet package manager. It is very easy to use.

Sign up to request clarification or add additional context in comments.

2 Comments

also do you know of any resources re learning EPPlus? edit: nvm found some stuff
EPPlus works directly with an Excel file (as far as I know xlsx file is just an xml). So it is very fast. Your approach is using OLE which is slowest possible. Only reason I would use it when i need to support older Excel file formats.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.