简体   繁体   English

读取 C# 中 Excel 的单元格值,null 引用异常

[英]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.我正在尝试从 Excel 表中读取单元格值,并使用 Value 和 Value2 来查看单元格值。 It keeps throwing "System.NullReferenceException: 'Object reference not set to an instance of an object.'" Error.它不断抛出“System.NullReferenceException:'对象引用未设置为 object 的实例。'”错误。 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.我知道它读取的文件路径和 Excel 表是正确的。

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它无法检索 Excel 范围并给出 NullReferenceException

Message:信息:

System.NullReferenceException: Object reference not set to an instance of an object. System.NullReferenceException:Object 引用未设置为 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. 1.- 使用“/”而不是“\”作为路径。 Windows doesn´t like those. Windows 不喜欢那些。

2.- I had to add the Excel.Workbook and Excel.Worksheet because the compiler warned me 2.-我必须添加 Excel.Workbook 和 Excel.Worksheet 因为编译器警告我

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!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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