0

I collect a data out of a excel sheet like this (using Interop Excel):

var tmp = WorkSheet.Range["G2", "AG2"].Value2;

Now my problem is that I can't get to the data because the type of tmp is object {object[,]}. How can i get access to the data?

enter image description here

Lars
  • 11
  • 2

1 Answers1

2

Based on your image from the debugger all the values are in double, so you can safely cast to double.

var tmp = (object[,])WorkSheet.Range["G2", "AG2"].Value2;
// Create a copy of the array
double[,] doubles = new double[tmp.GetLength(0), tmp.GetLength(1)];
for (int x = 0; x < tmp.GetLength(0); x++)
{
    for (int y = 0; y < tmp.GetLength(1); y++)
    {
        doubles[x, y] = (double)tmp[x, y];
    }
}
// Or inline
double value = (double)tmp[x,y]
Tomasz Juszczak
  • 2,276
  • 15
  • 25
  • 1
    Array.Copy may work well and is less to type than a loop. `var doubles = new double[tmp.GetLength(0), tmp.GetLength(1)];` `Array.Copy(tmp, doubles, tmp.Length);` – Youssef13 Jan 13 '21 at 13:31
  • @Youssef13 If i try to use your code, i got a error CS1061 "object" does not contain a definition for "GetLength". – Lars Jan 13 '21 at 13:42
  • 1
    @Lars Consider casting `tmp` to `object[,]` first. – Youssef13 Jan 13 '21 at 13:44