ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   efficient / easy way to convert an Excel.Range to a double[,] (https://www.excelbanter.com/excel-programming/402900-efficient-easy-way-convert-excel-range-double%5B-%5D.html)

My interest

efficient / easy way to convert an Excel.Range to a double[,]
 
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.

JLGWhiz

efficient / easy way to convert an Excel.Range to a double[,]
 
Check out CDbl function. CDbl(Range("A1").Value) converts the value in cell
A1 to double.

"My interest" wrote:

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.


My interest

efficient / easy way to convert an Excel.Range to a double[,]
 
Sorry for not making it clealy. I am developing in C# and the
emphasis is to convert multiple cells in one go.

If we know all cells in a range contain numeric values, it will be
inefficient and slow in accessing / converting one cell by one cell.


My interest

efficient / easy way to convert an Excel.Range to a double[,]
 
Just to make it clearer, the emphasis is how to convert multiple cells
in a ranger efficiently (e.g. in one call)


Kalpesh[_2_]

efficient / easy way to convert an Excel.Range to a double[,]
 
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

My interest

efficient / easy way to convert an Excel.Range to a double[,]
 
On Dec 18, 1:39 am, Kalpesh wrote:
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.

Kalpesh[_2_]

efficient / easy way to convert an Excel.Range to a double[,]
 
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


All times are GMT +1. The time now is 03:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com