0

How to set value 2D array object from excel sheet c#

object[,] DataRange = (object[,])xlrange.Value2;

I am getting error "Unable to cast object of type 'System.String' to type 'System.Object[,]".

I need solution for same

1
  • Instead of object have you then tried to use strings? Commented May 8, 2014 at 5:41

2 Answers 2

2

If your case, xlrange is probably a single cell. Try testing it with if(xlrange.Count > 1). You will need to handle single cells separately.

Sign up to request clarification or add additional context in comments.

1 Comment

I want to use multidimensional array objects strings is working
0

May I suggest you capture the case of it being a single cell and cast it into the object array. Then you can work on it from there consistently.

Please note another gotcha you will run into here is that 1D Ranges in excel will result in 0 based object arrays returned where 2D Ranges will be 1 based.

I suggest identifying the type, always casting to a 2D 1 based, then work consistently on the 2d 1 based arrays, and convert back if/when you need to write it back into excel.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.