Cells name

Mar 18, 2010 at 9:54 AM
Edited Mar 18, 2010 at 4:13 PM

I looking for something that gives me the "name" of a cell:

 

string fromCell = CellName(2, 2); // C3 cell
string toCell = CellName(2, 32); // AG3 cell

cell.SetCellFormula("SUM(" + fromCell + ":" + toCell + ")"); // SUM(C3:AG3) formula

 

Maybe I'm not so good with search tool but... I can't find! So, let's code!

 

    public string CellName(int row, int column)
{
string cellName = "A1";
try
{
string letter = "";
string number = "";

if (column <= 25)
letter = Convert.ToString((char)('A' + column));
else if (column > 25 && column <= 51)
{
int columnT = column - (26 * 1);
letter = "A";
string secondLetter = Convert.ToString((char)('A' + columnT));
letter += secondLetter;
}
else if (column > 51 && column <= 77)
{
int columnT = column - (26 * 1);
letter = "B";
string secondLetter = Convert.ToString((char)('A' + columnT));
letter += secondLetter;
}

number = Convert.ToString(row + 1);

cellName = letter + number;
}
catch
{

}
return cellName;
}

 

Hoping will be useful!