原 excel 将列号和字母相互转换工具类
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1242
工具类:
package com.mszl.utils;
public class ExcelColumnUtils {
/**
* 将Excel中的ABCD列转换成具体的数据
*/
public static int excelColStrToNum(String column) {
int num = 0;
int result = 0;
int length =column.length();
for(int i = 0; i < length; i++) {
char ch = column.charAt(length - i - 1);
num = (int)(ch - 'A' + 1) ;
num *= Math.pow(26, i);
result += num;
}
return result;
}
/**
* 将具体的数据转换成Excel中的ABCD列
*/
public static String excelColIndexToStr(int columnIndex) {
if (columnIndex <= 0) {
return null;
}
String columnStr = "";
columnIndex--;
do {
if (columnStr.length() > 0) {
columnIndex--;
}
columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;
columnIndex = (int) ((columnIndex - columnIndex % 26) / 26);
} while (columnIndex > 0);
return columnStr;
}
public static void main(String[] args) {
int colIndex = 8;
String colstr = excelColIndexToStr(colIndex);
System.out.println("下标为"+colIndex+"转换成列:"+colstr);
}
}
执行结果:
下标为8转换成列:H
2020-08-04 12:58:57 阅读(854)
名师出品,必属精品 https://www.91mszl.com