Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming, microsoft.public.dotnet.languages.csharp
external usenet poster
 
Posts: 14
Default 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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default 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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default 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.

  #4   Report Post  
Posted to microsoft.public.excel.programming, microsoft.public.dotnet.languages.csharp
external usenet poster
 
Posts: 14
Default 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)

  #5   Report Post  
Posted to microsoft.public.excel.programming, microsoft.public.dotnet.languages.csharp
external usenet poster
 
Posts: 15
Default 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


  #6   Report Post  
Posted to microsoft.public.excel.programming, microsoft.public.dotnet.languages.csharp
external usenet poster
 
Posts: 14
Default 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.
  #7   Report Post  
Posted to microsoft.public.excel.programming, microsoft.public.dotnet.languages.csharp
external usenet poster
 
Posts: 15
Default 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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert excel (.xls) to tab delimited (.txt) without double quotat Andrea Excel Discussion (Misc queries) 3 January 13th 09 11:11 PM
is there an easy way to convert an word xp file to excel xp? Maniacal Zebra Excel Discussion (Misc queries) 1 June 25th 07 08:05 AM
most efficient large range summing? jonigr Excel Programming 5 May 10th 07 06:26 AM
Can anyone help me convert to more efficient code? slo Excel Programming 2 August 4th 06 05:01 PM
more efficient way to lookup a range? [email protected] Excel Discussion (Misc queries) 1 May 12th 06 05:41 AM


All times are GMT +1. The time now is 11:52 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"