tags:

views:

144

answers:

1

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?

+2  A: 

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.

Zach Johnson
the NullReferenceException happens at string cellValue = rangeValue.ToString();because range.Value2 is null. i know my sheet is good but i think sheet.Cells[row, column] is the real culprit.
CurlyFro
I think then, that a null `range.Value2` indicates that the cell does not have a value. If you look at the cell in Excel, is it empty?
Zach Johnson
Also, Excel interop uses 1-based indexing. The problem could be that you are using zero-based indexes which reference an empty cell. If that is the case, then all you need to do is use `row + 1` and `column + 1` to get the correct cell.
Zach Johnson
also you can try using get_value instead of Value2
Stan R.
thanks!i was trying to reference an empty cell.
CurlyFro