# Excel Column

• 04-12-2008, 01:16 PM
Gajesh Tripathi
Excel Column
Hi

Presently, I am using excel interface with JAVA using jcom library

As you know, Excel has columns: A,B,C....Z,AA,AB,AC,......,AZ,BA,BB,....BZ,CA... and so on

My problem is that I want to create a function in JAVA whose prototype is:
you can understand working of this function by following examples:
getColName("H",0) returns "H", getColName("H",3) returns "K"
getColName("Y",3) returns "AB",getColName("AZ",5) returns "HE"
And So On

I hope, u understand that what kind of function, I want to create

Please tell me the logic behind it.

Thanx
GAJESH TRIPATHI
• 04-12-2008, 06:10 PM
Chris.Brown.SPE
This should do what you want. This however does not have functionality to accept ZZ and output AAA. I didnt bother seeing as how excel wont even reach ZZ. This shouldnt be hard to add though if someone wants to put it in.

Code:

static String getColName(String temp, int over) {
int value;
int last;
char[] tempch = temp.toCharArray();
last = tempch.length - 1;
value = tempch[last] + over;
tempch[last] = (char) value;
if (value > (int) 'Z') {
tempch[last] = (int) 'A';
over = (value - (int) 'Z' - 1);
if (last == 0) {
tempch = (("A") + String.copyValueOf(tempch)).toCharArray();
} else {
tempch[last - 1] = (char) ((int) tempch[last - 1] + 1);
}
tempch = getColName(String.copyValueOf(tempch), over).toCharArray();
}

return String.copyValueOf(tempch);
}

• 04-13-2008, 02:26 PM
Gajesh Tripathi
Best Logic for Excel Column
Your logic is very simple. Thanx for it.

However here is exact solution of this problem: