Page 1 of 1

Excel into 2D array

Posted: Wed Mar 06, 2013 11:14 pm
by nhassan
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,

Re: Excel into 2D array

Posted: Thu Mar 07, 2013 7:54 am
by Support Team
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

Re: Excel into 2D array

Posted: Thu Mar 07, 2013 2:52 pm
by nhassan
Hi,
I need to read from Excel into a 2D array. All the posts are to write to Excel.

Thanks,

Re: Excel into 2D array

Posted: Thu Mar 07, 2013 3:33 pm
by Ciege
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...

Re: Excel into 2D array

Posted: Thu Mar 07, 2013 5:53 pm
by nhassan
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.

Re: Excel into 2D array

Posted: Thu Mar 07, 2013 6:14 pm
by Ciege
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