简体   繁体   中英

Reading Cell Value of Excel in C#, null reference exception

I am trying to read the cell value from an Excel sheet, and used Value and Value2 to see the Cell value. It keeps throwing "System.NullReferenceException: 'Object reference not set to an instance of an object.'" Error. I am unable to figure out where is the issue in code.

I know the file path and the Excel sheet it's reading is correct.

public String readEDriver()
{
    int nRows = 1;
    int nCols = 1;
    String driverLoc = null;
    Excel.Application excelApp = new Excel.Application();
    if (excelApp != null)
    {
        Workbook excelWorkbook;
        excelWorkbook = excelApp.Workbooks.Open("C:\\A\\Config.xlsx", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        Worksheet excelWorksheet;
        excelWorksheet = (Excel.Worksheet)excelWorkbook.Worksheets[1];
        excelWorksheet.Activate();
        String eName =excelWorksheet.Name;
        if (excelWorksheet == null)
        {
            throw new Exception(string.Format("Named worksheet ({0}) not found.", excelWorksheet));
        }
        else  
        {
            var cellVal = ((Microsoft.Office.Interop.Excel.Range)excelWorksheet.Cells[nRows, nCols]).Value2;

            if (excelWorksheet.Cells[nRows,nCols]!=null)
            {
                cellVal = ((Microsoft.Office.Interop.Excel.Range)excelWorksheet.Cells[nRows, nCols]).Value2;
                driverLoc = cellVal.ToString();
            }
        }

        excelWorkbook.Close();
        excelApp.Quit();    
    }
    return driverLoc;
}

it breaks at the

var cellVal = ((Microsoft.Office.Interop.Excel.Range)excelWorksheet.Cells[nRows, nCols]).Value2;

It fails to retrieve the Excel Range and gives NullReferenceException

Message:

System.NullReferenceException: Object reference not set to an instance of an object.
Stack Trace:

I tried your code because it seemed OK. It works for me with a couple of minor compiling topics:

1.- Use "/" instead od "\" for the paths. Windows doesn´t like those.

2.- I had to add the Excel.Workbook and Excel.Worksheet because the compiler warned me

Apart from that it works. Below your code with my slight corrections.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;


namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string aux = readEDriver();
            Console.WriteLine(aux);
            Console.ReadLine();
        }
        public static string readEDriver()
        {
            int nRows = 1;
            int nCols = 1;
            String driverLoc = null;
            Excel.Application excelApp = new Excel.Application();
            if (excelApp != null)
            {
                Excel.Workbook excelWorkbook;
                excelWorkbook = excelApp.Workbooks.Open("C:/Users/Usuario/Desktop/PruebasTxtFile.xlsx", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                Excel.Worksheet excelWorksheet;
                excelWorksheet = (Excel.Worksheet)excelWorkbook.Worksheets[1];
                excelWorksheet.Activate();
                String eName = excelWorksheet.Name;
                if (excelWorksheet == null)
                {
                    throw new Exception(string.Format("Named worksheet ({0}) not found.", excelWorksheet));
                }
                else
                {
                    var cellVal = ((Microsoft.Office.Interop.Excel.Range)excelWorksheet.Cells[nRows, nCols]).Value2;

                    if (excelWorksheet.Cells[nRows, nCols] != null)
                    {
                        cellVal = ((Microsoft.Office.Interop.Excel.Range)excelWorksheet.Cells[nRows, nCols]).Value2;
                        driverLoc = cellVal.ToString();
                    }
                }

                excelWorkbook.Close();
                excelApp.Quit();
            }
            return driverLoc;
        }
    }
}

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM