求解Excel工作表的列号

题目

求解Excel工作表的列号
A B C *** U V W X Y Z AA AB
入参:字符串
出参:数字,第几列
比如:
AB->28
Y->25

解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 编写方法求解Excel工作表 列号
*/
public class Main {
/**
* A B C *** U V W X Y Z AA AB
* 入参:字符串
* 出参:数字,第几列
*/

public static void main(String[] args) {
System.out.println(get("Y"));
}

public static int get(String s) {
//思路26进制转10进制
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
double result = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char c = s.charAt(i);
int index = str.indexOf(c) + 1;
result = result + index * Math.pow(26, s.length() - 1 - i);
}
return (int) result;
}
}

求解Excel工作表的列号
https://leehoward.cn/2022/02/23/求解Excel工作表的列号/
作者
lihao
发布于
2022年2月23日
许可协议