0

I have excel file that contains 10 columns. i have to select multiple columns based on their names then copy all values in one array.

first step i tried to get the columns index

string columnsName;
var columnsNumbers = new List<int>();
for (int i = 1; i < range.Columns.Count; i++)
{
   columnsName = ((Range) range.Cells[1, i]).Value2;
   if(columnsName.Equals("Empl, "+DateTime.Now.Year,StringComparison.OrdinalIgnoreCase))
     columnsNumbers.Add(i);
   if (columnsName.Equals("Test, " + DateTime.Now.Year, StringComparison.OrdinalIgnoreCase))
      columnsNumbers.Add(i);
   if (columnsName.Equals("Jour, " + DateTime.Now.Year, StringComparison.OrdinalIgnoreCase))
      columnsNumbers.Add(i);
   if (columnsName.Equals("Actual, Other Suppliers, " + DateTime.Now.Year, StringComparison.OrdinalIgnoreCase))
      columnsNumbers.Add(i);
   if (columnsName.Equals("FutureR, " + DateTime.Now.Year+1, StringComparison.OrdinalIgnoreCase))
      columnsNumbers.Add(i);
 }

Now that i added the target indexes in a list, how can i got the values of the stored column indexes and store all in 2d array?

Maro
  • 2,579
  • 9
  • 42
  • 79
  • another issue with that code ... you use double dots ... and i think that might be COM interop, so you might want to read this... http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects – DarkSquirrel42 Dec 10 '13 at 13:31
  • @DarkSquirrel42 I don't see what this link has to do with my question? i don't have issue releasing the com object ... etc – Maro Dec 10 '13 at 13:41
  • if you are using COM interop for accessing excel you'll probably get that problem sooner or later ... in the mean time, see the answer below – DarkSquirrel42 Dec 10 '13 at 13:42
  • Well, this is not the issue at the moment – Maro Dec 10 '13 at 13:42

1 Answers1

0

google helps...

here you can see how to get a range between two cells

together with this (which does quite the opposite, writing an array to a range) you will probably find out how to read a range into an array ...

once you have all your arrays for the columns, all you have to do is to find out which dimensions your final array should have, create it, and then start to copy

Community
  • 1
  • 1
DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31