Monday, March 8, 2010

Convert Excel Columns to Number

A code snippet that contains a utility method to get the column number from a column character, e.g. A -> 0 or AA -> 26

public int getColumnCount(String column, int multiplyFactor){
int col=0;
int minusFactor = -10;
if(column.length()==1){
col = minusFactor + Character.getNumericValue(column.charAt(0));
}else{
multiplyFactor += minusFactor + Character.getNumericValue(column.charAt(0)) ;
col = 26 * multiplyFactor + getColumnCount(column.substring(1),multiplyFactor+25);
}
return col;
}

Usage:
int colCount = getColumnCount(String columnChar,1)

Hope this helps!

No comments: