How to read single Excel cell value

前端 未结 7 1943
情深已故
情深已故 2020-11-29 04:04

I have excel file with sheet1 that has a value I need to read on row 2 and column 10. Here is my code.

Excel.Workbook excelWorkbook = excelApp.Workbooks.Ope         


        
7条回答
  •  旧时难觅i
    2020-11-29 04:30

    The issue with reading single Excel Cell in .Net comes from the fact, that the empty cell is evaluated to a Null. Thus, one cannot use its .Value or .Value2 properties, because an error shows up.

    To return an empty string, when the cell is Null the Convert.ToString(Cell) can be used in the following way:

    Excel.Workbook wkb = Open(excel, filePath);
    Excel.Worksheet wk = (Excel.Worksheet)excel.Worksheets.get_Item(1);
    
    for (int i = 1; i < 5; i++)
    {
        string a = Convert.ToString(wk.Cells[i, 1].Value2);
        Console.WriteLine(a);
    }
    

提交回复
热议问题