Excel into 2D array

Ask general questions here.
nhassan
Posts: 10
Joined: Tue Jan 22, 2013 10:05 pm

Excel into 2D array

Post by nhassan » Wed Mar 06, 2013 11:14 pm

Hi,
I was wondering if anybody can share a code snippet to read an excel file into a 2D array. I don't want to use the data connector since I have to do some manipulation of the data in the array.

Or is it doable using the data connector as well?


Thanks,

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Excel into 2D array

Post by Support Team » Thu Mar 07, 2013 7:54 am

Hello,

Unfortunately it is not possible to write values into the excel sheet using the data connector.
In order to write values into you excel file you can use the .NET framework directly.
There are several posts about this topic in the forum. Here are some of them:
http://www.ranorex.com/forum/writing-ex ... t1605.html
http://www.ranorex.com/forum/how-to-wri ... t3258.html
http://www.ranorex.com/forum/writing-th ... t1702.html

Regards,
Bernhard
.
Image

nhassan
Posts: 10
Joined: Tue Jan 22, 2013 10:05 pm

Re: Excel into 2D array

Post by nhassan » Thu Mar 07, 2013 2:52 pm

Hi,
I need to read from Excel into a 2D array. All the posts are to write to Excel.

Thanks,

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Excel into 2D array

Post by Ciege » Thu Mar 07, 2013 3:33 pm

Have a look at the methods I posted here: http://www.ranorex.com/forum/my-excel-f ... t3265.html

They might help you out or at least get you going in the right direction...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

nhassan
Posts: 10
Joined: Tue Jan 22, 2013 10:05 pm

Re: Excel into 2D array

Post by nhassan » Thu Mar 07, 2013 5:53 pm

Hi Ciege,
Thanks for sharing the ExcelFramework.

When I use the ConvertToStringArray(myvalues), it complains that "The name 'ConvertToStringArray' does not exist in the current context (CS0103)".

I have the following references in my usercode:

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;


Please advise.

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Excel into 2D array

Post by Ciege » Thu Mar 07, 2013 6:14 pm

I think that method was not included in the solution I posted for some reason...
Here is that method:

Code: Select all

        public static string[] ConvertToStringArray(System.Array values)
        {
            string[] newArray = new string[values.Length];

            int index = 0;
            for (int i = values.GetLowerBound(0);
                  i <= values.GetUpperBound(0); i++)
            {
                for (int j = values.GetLowerBound(1);
                          j <= values.GetUpperBound(1); j++)
                {
                    if (values.GetValue(i, j) == null)
                    {
                        newArray[index] = "";
                    }
                    else
                    {
                        newArray[index] = (string)values.GetValue(i, j).ToString();
                    }
                    index++;
                }
            }
            return newArray;
        } //End ConvertToStringArray
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...