created functions to go the other direction row,col = cellref "A1"

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352534 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew C. Oliver 2002-04-28 18:23:06 +00:00
parent 9683424c14
commit ab97dacce6

View File

@ -20,8 +20,8 @@ public class ReferenceUtil {
* takes in a cell reference string A1 for instance and returns an integer * takes in a cell reference string A1 for instance and returns an integer
* array with the first element being the row number and the second being * array with the first element being the row number and the second being
* the column number, all in 0-based base 10 format. * the column number, all in 0-based base 10 format.
* * @param reference a cell reference such as A1 or AA1 or IV1
* @return xyarray row and column number * @return xyarray int array containing row and column number
*/ */
public static int[] getXYFromReference(String reference) { public static int[] getXYFromReference(String reference) {
int[] retval = new int[2]; int[] retval = new int[2];
@ -31,6 +31,36 @@ public class ReferenceUtil {
return retval; return retval;
} }
/**
* takes in a row and column and returns a string cellref
* @param row the 0 based row such as 0
* @param col the 0 based col number such as 1
* @return cellreference string such as B1
*/
public static String getReferenceFromXY(int row, int col) {
String retval = convertNumToColString(col) + ""+(row+1);
return retval;
}
/**
* takes in a 0-based base-10 column and returns a ALPHA-26 representation
*/
private static String convertNumToColString(int col) {
String retval = null;
int mod = col % 26;
int div = col / 26;
char small=(char)(mod + 65);
char big = (char)(div + 64);
if (div == 0) {
retval = ""+small;
} else {
retval = ""+big+""+small;
}
return retval;
}
/** /**
* takes in a column reference portion of a CellRef and converts it from * takes in a column reference portion of a CellRef and converts it from
* ALPHA-26 number format to 0-based base 10. * ALPHA-26 number format to 0-based base 10.