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
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
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.
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.