From ab97dacce65fe4bf2d8b2841d19a307463e4cf42 Mon Sep 17 00:00:00 2001 From: "Andrew C. Oliver" Date: Sun, 28 Apr 2002 18:23:06 +0000 Subject: [PATCH] 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 --- .../apache/poi/hssf/util/ReferenceUtil.java | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/poi/hssf/util/ReferenceUtil.java b/src/java/org/apache/poi/hssf/util/ReferenceUtil.java index 643d8ad2f..87aa751e5 100644 --- a/src/java/org/apache/poi/hssf/util/ReferenceUtil.java +++ b/src/java/org/apache/poi/hssf/util/ReferenceUtil.java @@ -20,8 +20,8 @@ public class ReferenceUtil { * 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 * the column number, all in 0-based base 10 format. - * - * @return xyarray row and column number + * @param reference a cell reference such as A1 or AA1 or IV1 + * @return xyarray int array containing row and column number */ public static int[] getXYFromReference(String reference) { int[] retval = new int[2]; @@ -31,6 +31,36 @@ public class ReferenceUtil { 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 * ALPHA-26 number format to 0-based base 10. @@ -74,5 +104,5 @@ public class ReferenceUtil { retval[1] = reference.substring(loc); return retval; } - + }