C# & Excel: Value2 has no definition

I needed a way to read an Excel file through c#. I found an example https://coderwall.com/p/app3ya/read-excel-file-in-c

I created a standalone program to test it and it worked as I had expected. I added the code as a subprogram into an existing program and encountered errors.

The first error, "Cannot implicitly convert type 'object' to Microsoft.Office.Interop.Excel._Worksheet. An explicit conversion exists."

The second error, "'Object' does not contain a definition for 'Value2'".

I fixed the first error with adding (Excel._Worksheet) to xlWorkbook.Sheets[1];

I can't figure out the second one on Value2 and need your help: "Error 98 'object' does not contain a definition for 'Value2' and no extension method 'Value2' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)"

Here's my code:

using System;
using System.Collections.Generic;
using System.Collections;ArrayList, ListBox, ComboBox, etc.
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XView.Properties;
using System.Reflection;
using ZedGraph;
using System.IO;
using System.Management;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

private void readExcelFile(ArrayList al)
        string rowItems = string.Empty;

        //Create COM Objects. Create a COM object for everything that is referenced
        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"W:\acad\x-pak\XPak setting_tool_charts.xlsx");
        Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
        Excel.Range xlRange = xlWorksheet.UsedRange;

        int rowCount = xlRange.Rows.Count;
        int colCount = xlRange.Columns.Count;

        //iterate over the rows and columns and print to the console as it appears in the file - Excel is not zero based!!
        //---start at i = 4 since in Rick's Excel file, that's where the data begins!
        for (int i = 4; i <= rowCount; i++)
            rowItems = string.Empty;

            for (int j = 1; j <= colCount; j++)
                //new line
                if (j == 1)

                //write the value to the console
                if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                    //Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");

                    if (j == 1) //new line
                        rowItems = xlRange.Cells[i, j].Value2.ToString();
                        rowItems += "^" + xlRange.Cells[i, j].Value2.ToString();


        //rule of thumb for releasing com objects:
        //  never use two dots, all COM objects must be referenced and released individually
        //  ex: [somthing].[something].[something] is bad

        //release com objects to fully kill excel process from running in the background

        //close and release

        //quit and release

I think it is relevant to Microsoft.Office.Interop.Excel version. I have never previously used code block following ways to solve the same mistake. I think it will work for him.

   object _xVal;

    _xVal= ((Excel.Range)xlWorksheet.Cells[i, j]).Value2;

C Sharp (programming language), Learn Microsoft's popular programming language C#, used to make websites, mobile apps, video games, VR, and more! C# (pronounced see sharp, like the musical note C♯, but written with the number sign) is a general-purpose, multi-paradigm programming language encompassing strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.

The cause of this problem was the Target Framework! It was using .Net 3.5 because the main program was created in 2008. This subprogram was added today and it assumes that "Embed Interop Types = true" (Property of Reference: Microsoft.Office.Interop.Excel).

I went to the Project Properties and set the Target Framework to ".Net Framework 4 Client Profile". That generated 43 errors (can't remember the error name but indicated that maybe I needed to add a reference to Microsoft.CSharp.dll). I added the Microsoft.CSharp reference and all the errors disappeared!

C# Tutorial (C Sharp), VS Code supports debugging of C# applications running on either .NET Core or Mono. For detailed instructions on: .NET Core debugging - see the Microsoft C#� Visual Studio tutorials | C#. Create C# apps with Visual Studio. Get started How-To Guide Install Visual Studio; Get Started Start a guided tour

I tried this same example tutorial and ran into the same issue. I resolved the issue in following method:

  1. First, create a variable like this:

    var sno = (Excel.Range)xlWorksheet.Cells[i,1];

  2. And then access the Value2 property like this:

    string serialno = sno.Value2.ToString();

I am using .net framework 4.5

Learn C#, C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg. Write your first lines of C# code; Store and manipulate data to modify its type and appearance; Execute built-in functionality available from libraries of code; Add logic to your code to enable complex business functionality; Once you complete this learning path, you will have a great foundation to build upon in subsequent C# Learning Paths.

C#, This course will give you a full introduction into all of the core concepts in C# (aka C Sharp Duration: 4:31:09 Posted: Sep 6, 2018 This tutorial teaches you C# interactively, using your browser to write C# and see the results of compiling and running your code. It contains a series of lessons that begin with a "Hello World" program. These lessons teach you the fundamentals of the C# language. As you explore C# (or any

C# programming with Visual Studio Code, What is C#? It's a powerful and widely used programming language that you can use to make Duration: 1:24 Posted: Sep 23, 2019 A C# application consists of classes and their members. Classes and other types exist in namespaces but can also be nested inside other classes. Main method. Whether it is a console or a graphical interface application, the program must have an entry point of some sort. The entry point of the C# application is the Main method. There can only be

C# Tutorial, C# is the most commonly used language for leveraging the .NET Framework. As such, learning C# is a springboard to creating enterprise systems, desktop� Provides extension methods for tuples to interoperate with language support for tuples in C#. Type: Represents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types. TypeAccessException