转自:https://blog.csdn.net/zwhfyy/article/details/45562589
//根据长度生成字段名集合,A-Z...
public static List<String> getColumnList(int length){
List<String> list = new ArrayList<>();
for (int i = 0; i < length; i++) {
String column = getKey(i);
list.add(column);
}
return list;
}
//java 实现生成excel表头,A-Z;AA-ZZ;AAA-ZZZ支持无限
public static String getKey(int index){
String colCode = "";
char key='A';
int loop = index / 26;
if(loop>0){
colCode += getKey(loop-1);
}
key = (char) (key+index%26);
colCode += key;
return colCode;
}
//测试
public static void main(String[] args) throws IOException {
String key = getKey(26);
System.out.println(key);
List<String> columnList = getColumnList(26);
System.out.println(columnList);
}
输出:
AA
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]