Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the value of a cell by row index and column index

Tags:

c#

excel

currently i'm doing this:

string cellValue = sheet.get_Range("A12", _missing).Value2.ToString();

this works but i really need to select a cell by row and column index.

i get a null exception when i try

string cellValue = ((Range)sheet.Cells[row, column]).Value2.ToString();

any ideas?

like image 658
CurlyFro Avatar asked Feb 19 '10 20:02

CurlyFro


People also ask

How do I extract specific data from a cell in Excel?

=LEFT(B1,2) to extract the first 2 characters of the cell B1. =RIGHT(B1,8) to extract the last 8 characters of the cell B1. =MID(B1,4,2) to extract the 2 characters following the 4th character in B1. To apply the changes to the cells below, drag down the blue square.

How do you use INDEX reference in Excel?

The reference form of the Excel INDEX function returns the cell reference at the intersection of the specified row and column. reference - is one or several ranges. If you are entering more than one range, separate the ranges by commas and enclose the reference argument in parentheses, for example (A1:B5, D1:F5).


1 Answers

Where does the ArgumentNullException occur? Try separating out your code like this and step through it:

object rangeObject = sheet.Cells[row, column];
Range range = (Range)rangeObject;
object rangeValue = range.Value2;
string cellValue = rangeValue.ToString();

This will show you where the null object is.

like image 195
Zach Johnson Avatar answered Sep 24 '22 14:09

Zach Johnson