Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
My interest - 17 Dec 2007 23:34 GMT
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 - 18 Dec 2007 01:51 GMT
Just to make it clearer, the emphasis is how to convert multiple cells
in a ranger efficiently (e.g. in one call)
Kalpesh - 18 Dec 2007 06:39 GMT
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 - 18 Dec 2007 14:05 GMT
> While I don't have the environment to do the C# stuff in Excel -
> value2 returns a jagged array
[quoted text clipped - 5 lines]
>
> Kalpesh

No,unfortunately this does not work.  In C#, it seems that Value2
returns a Object[,] instead of a jagged array.
Kalpesh - 19 Dec 2007 14:25 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.