1

I have 2 integer values which represents a cell index in an excel sheet(excel 2010) and I need to create the corresponding excel cell representation(i.e 0,0 represents A1) out of it - a basic string.

For example: - If the cell index is 0,0 then my code should output A1 (represents the first cell)

  • If the cell index is 0,1 then it should output B1

  • If the cell index is 0,27 then it should output AA1

  • If the cell index is 0,28 then it should output AB1 ..and so on.

I got it correct until A** and not beyond that from the below code..how can I modify the logic to output the correct excel cell representation for other indexes i.e beyond A** and it goes to B** and so on..

Code:

 class Program
    {
        static void Main(string[] args)
        {

            for (int row  = 0; row < 2; row++)
            {
                for (int column = 0; column < 50; column++)
                {
                    var header = Headers(column + 1, row).Skip(column).First().ToString();

                    Console.WriteLine(header);
                }
            }

            Console.ReadLine();
        }

        public static IEnumerable<string> Headers(int column, int row)
        {
            for (int index = 0; index < column; index++)
            {
                if (index >= 26)
                {
                    var firstInt = index / 26 - 1;
                    var firstAplhabet = ((char)(firstInt + 65)).ToString();
                    var secondInt = index - 26;
                    var secondAlphabet = ((char)(secondInt + 65)).ToString();

                    yield return firstAplhabet + secondAlphabet + (row + 1);
                    continue;
                }

                var alphabet = ((char)(index + 65)).ToString();

                yield return alphabet + (row + 1);
            }
        }
    }
leppie
  • 115,091
  • 17
  • 196
  • 297
Mike
  • 3,204
  • 8
  • 47
  • 74

0 Answers0