0

Welll, I hope this won't be a duplicate question, but I'm managing in C# a range of cells in Excel, i have filled an array with a for cycle; to simplify let's say my array is more or less

// row,column, i.e. [3,1] is row 3, col 1
Range[] arrayOfRanges;
arrayOfRanges[0] = xlsSheet.Range[ xlsSheet.Range[3,1] , xlsSheet.Range [45,8] ];
arrayOfRanges[1] = xlsSheet.Range[ xlsSheet.Range[46,1] , xlsSheet.Range [89,8] ];
arrayOfRanges[2] = xlsSheet.Range[ xlsSheet.Range[90,1] , xlsSheet.Range [132,8] ];
arrayOfRanges[3] = xlsSheet.Range[ xlsSheet.Range[133,1] , xlsSheet.Range [176,8] ];
arrayOfRanges[4] = xlsSheet.Range[ xlsSheet.Range[177,1] , xlsSheet.Range [207,8] ];

Is there a way to use Console.Writeline to have an output similar to this:

[ 3 , 1]
[ 46 , 89]
...

I Tried with

for (int i = 1; i<=arrayOfRanges.Length; i++)
{
    Console.WriteLine("[ " + arrayOfRanges[i][0].Value + "," + arrayOfRanges[i][1].Value + " ]\n");
}

But it doesn't seem the correct way.

3
  • "doesn't seem correct"...why, what happens when you run it? Commented Jul 10, 2019 at 11:01
  • 1
    P.S. in Console.WriteLine (as opposed to Console.Write) you don't have to add \n at the end, it's already done for you Commented Jul 10, 2019 at 11:02
  • I'm sorry, i edited my question; what it prints is the CONTENT of the cells; i want the number that references the cells. For example, if I have a cell [1,3] ( that is C1) that contains "hello", i don't want to print "hello", but "[1 , 3]" Commented Jul 10, 2019 at 11:03

1 Answer 1

1

You could use the Column and Row properties of the Range type:

Console.WriteLine($"[{arrayOfRanges[i][0].Row}, {arrayOfRanges[i][0].Column}]");

(Also note how I used the $ sign to allow inline parameters inside the string.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It gave me an error, but with arrayOfRanges[i][1].Row (1 instead of 0) it gives me the first range correctly (in my example [3,1], [46,1], etc)! Now how to obtain the second one ([45,8], [89,8], etc)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.