package com.yanzuoguang.excel; /** * 单元格引用工具类 * * @author 颜佐光 */ public class CellReferenceUtil { /** * Input Coordination: C
* Output Column Number: 2
* * @param colName * @return colIndex: 0 based index */ public static int getColIndex(String colName) { colName = colName.toUpperCase(); int value = 0; for (int i = 0; i < colName.length(); i++) { int delta = colName.charAt(i) - 64; value = value * 26 + delta; } int colIndex = value - 1; return colIndex; } /** * Input column index: 2
* Output column name: C
* * @param colIndex * @return */ public static String getColName(int colIndex) { int quotient = (colIndex) / 26; if (quotient > 0) { return getColName(quotient - 1) + (char) ((colIndex % 26) + 65); } else { return "" + (char) ((colIndex % 26) + 65); } } /** * Input coord: C8
* Output Row Number: 7
* * @param rowName * @return rowIndex starts with 0 */ public static int getRowIndex(String rowName) { int rowIndex = Integer.parseInt(rowName) - 1; return rowIndex; } /** * Input rowIndex: 7
* Output rowName: 8
* * @param rowIndex * @return */ public static String getRowName(int rowIndex) { int rowName = rowIndex + 1; return String.valueOf(rowName); } /** * Input pos: col = 2, row = 7
* Output coord: C8
* * @param colIndex * @param rowIndex * @return */ public static String getCoordName(int colIndex, int rowIndex) { String colName = getColName(colIndex); String rowName = getRowName(rowIndex); return colName + rowName; } /** * Input coordName: C8
* Output colIndex: 2
* * @param coordName * @return colIndex: Starts with 0 */ public static int getColIndexByCoordName(String coordName) { String[] colAndRowName = splitColAndRow(coordName); String colName = colAndRowName[0]; return getColIndex(colName); } /** * Input coordName: C8
* Output rowIndex: 7
* * @param coordName * @return rowIndex: 0 based index */ public static int getRowIndexByCoordName(String coordName) { String[] colAndRowName = splitColAndRow(coordName); String rowName = colAndRowName[1]; return getRowIndex(rowName); } /** * Input coordName: C8
* Output : String[]{C, 8}
* * @param coordName * @return */ private static String[] splitColAndRow(String coordName) { int rowNumStartIndex = 0; for (int i = 0; i < coordName.length(); i++) { char ch = coordName.charAt(i); if (Character.isDigit(ch)) { rowNumStartIndex = i; break; } } String colName = coordName.substring(0, rowNumStartIndex); String rowName = coordName.substring(rowNumStartIndex); return new String[] {colName, rowName}; } }