efficient / easy way to convert an Excel.Range to a double[,]

M

My interest

Somewhat I remember I used to be able to do sth like the the following

double[,] values = (double[,])excel_range.Value2

But it seems to be not working any more, throwing an exception "cannot
convert System.Object[,] to System.Double[,]"

Of course, I can do iteration and, as a bottom line, using
Double.Parse(cell_value.ToString()). The question is there a more
efficient way to do the job?

I am using Office XP with PIA.

Thanks.
 
M

My interest

Just to make it clearer, the emphasis is how to convert multiple cells
in a ranger efficiently (e.g. in one call)
 
K

Kalpesh

While I don't have the environment to do the C# stuff in Excel -
value2 returns a jagged array
So, this should work

double[][] values = (double[][])excel_range.Value2

Does this help?

Kalpesh
 
M

My interest

While I don't have the environment to do the C# stuff in Excel -
value2 returns a jagged array
So, this should work

double[][] values = (double[][])excel_range.Value2

Does this help?

Kalpesh

No,unfortunately this does not work. In C#, it seems that Value2
returns a Object[,] instead of a jagged array.
 
K

Kalpesh

This works well.
When transferring data from object[,] to double[,] - use copy method &
specify length of the array.
In this case it is 4 (2 rows, 2 columns)

See the code below for example.

object[,] data = new object[2,2];
data[0,0] = 0.5d;
data[0,1] = 1.5d;
data[1,0] = 2.0d;
data[1,1] = 5.5d;

double[,] ddata = new double[2,2];
Array.Copy(data, ddata, 4);
Console.WriteLine("ok");
ddata[0,0] = 3.5;

Console.WriteLine(ddata[0,0]);
Console.WriteLine(ddata[0,1]);
Console.WriteLine(ddata[1,0]);
Console.WriteLine(ddata[1,1]);

HTH
Kalpesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top