91名师指路-头部
91名师指路

excel 将列号和字母相互转换工具类

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

工具类:

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

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部