colIndexToAlpha(27) -> return AA
alphaToColIndex(AA) -> return 27
// get cellReference’s column from column index
public string colIndexToAlpha(int colIndex) {
List<string> colNameList = new List<string>();
int num1 = Convert.ToInt32(colIndex) – 1;
int num2 = 0;
int num3 = 0;
while (true) {
num2 = num1 / 26;
num3 = num1 – 26 * num2;
colNameList.Insert(0, numToAlpha(num3));
if (num2 < 26) {
colNameList.Insert(0, numToAlpha(num2 – 1));
break;
}
num1 = num2;
}
return String.Join(“”, colNameList.ToArray());
}
private String numToAlpha(int number) {
if (number < 0) {
return “”;
}
Char c = (Char)(65 + number);
return c.ToString();
}
// get column index from cellReference’s column
public int alphaToColIndex(string name) {
int index = 0;
for (int i = 0; i < name.Length; i++) {
index *= 26;
index += alphaToNum(name[i]);
}
return index;
}
private int alphaToNum(char alpha) {
return alpha – 64;// A -> 1 Z -> 26
}
private int alphaToNum(string alpha) {
if (alpha.Length != 1) {
return 0;
}
return alpha[0] – 64; // A -> 1 Z -> 26
}