|
|
|
@ -1,10 +1,20 @@
|
|
|
|
|
package com.alibaba.excel.util; |
|
|
|
|
|
|
|
|
|
import java.util.Locale; |
|
|
|
|
import java.util.regex.Matcher; |
|
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
|
import org.apache.poi.ss.util.CellReference; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author jipengfei |
|
|
|
|
*/ |
|
|
|
|
public class PositionUtils { |
|
|
|
|
|
|
|
|
|
private static final Pattern CELL_REF_PATTERN = Pattern.compile("(\\$?[A-Z]+)?" + "(\\$?[0-9]+)?", |
|
|
|
|
Pattern.CASE_INSENSITIVE); |
|
|
|
|
private static final char SHEET_NAME_DELIMITER = '!'; |
|
|
|
|
|
|
|
|
|
private PositionUtils() {} |
|
|
|
|
|
|
|
|
|
public static int getRowByRowTagt(String rowTagt) { |
|
|
|
@ -30,35 +40,37 @@ public class PositionUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static int getRow(String currentCellIndex) { |
|
|
|
|
int row = 0; |
|
|
|
|
if (currentCellIndex != null) { |
|
|
|
|
String rowStr = currentCellIndex.replaceAll("[A-Z]", "").replaceAll("[a-z]", ""); |
|
|
|
|
row = Integer.parseInt(rowStr) - 1; |
|
|
|
|
int plingPos = currentCellIndex.lastIndexOf(SHEET_NAME_DELIMITER); |
|
|
|
|
String cell = currentCellIndex.substring(plingPos + 1).toUpperCase(Locale.ROOT); |
|
|
|
|
Matcher matcher = CELL_REF_PATTERN.matcher(cell); |
|
|
|
|
if (!matcher.matches()) { |
|
|
|
|
throw new IllegalArgumentException("Invalid CellReference: " + currentCellIndex); |
|
|
|
|
} |
|
|
|
|
return row; |
|
|
|
|
String row = matcher.group(2); |
|
|
|
|
return Integer.parseInt(row) - 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static int getCol(String currentCellIndex) { |
|
|
|
|
int col = 0; |
|
|
|
|
if (currentCellIndex != null) { |
|
|
|
|
|
|
|
|
|
char[] currentIndex = currentCellIndex.replaceAll("[0-9]", "").toCharArray(); |
|
|
|
|
for (int i = 0; i < currentIndex.length; i++) { |
|
|
|
|
col += (currentIndex[i] - '@') * Math.pow(26, (currentIndex.length - i - 1)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return col - 1; |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static int getCol(String currentCellIndex, Integer before) { |
|
|
|
|
int col = 0; |
|
|
|
|
if (currentCellIndex != null) { |
|
|
|
|
int plingPos = currentCellIndex.lastIndexOf(SHEET_NAME_DELIMITER); |
|
|
|
|
String cell = currentCellIndex.substring(plingPos + 1).toUpperCase(Locale.ROOT); |
|
|
|
|
Matcher matcher = CELL_REF_PATTERN.matcher(cell); |
|
|
|
|
if (!matcher.matches()) { |
|
|
|
|
throw new IllegalArgumentException("Invalid CellReference: " + currentCellIndex); |
|
|
|
|
} |
|
|
|
|
String col = matcher.group(1); |
|
|
|
|
|
|
|
|
|
char[] currentIndex = currentCellIndex.replaceAll("[0-9]", "").toCharArray(); |
|
|
|
|
for (int i = 0; i < currentIndex.length; i++) { |
|
|
|
|
col += (currentIndex[i] - '@') * Math.pow(26, (currentIndex.length - i - 1)); |
|
|
|
|
if (col.length() > 0 && col.charAt(0) == '$') { |
|
|
|
|
col = col.substring(1); |
|
|
|
|
} |
|
|
|
|
if (col.length() == 0) { |
|
|
|
|
return -1; |
|
|
|
|
} else { |
|
|
|
|
return CellReference.convertColStringToIndex(col); |
|
|
|
|
} |
|
|
|
|
return col - 1; |
|
|
|
|
} else { |
|
|
|
|
if (before == null) { |
|
|
|
|
before = -1; |
|
|
|
|