Excel Sheet Column Number

1. Question

将excel表中的列标题(A、B、C...)转换为对应的列数字返回。

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 

2. Solution

是进制转化问题,将用A~Z表示的26进制数转化为10进制数。

    public int titleToNumber( String s ){
        int result=0;
        int pow = (int) Math.pow(26, s.length()-1);
        for( int i=0; i<s.length(); i++){
            char c = s.charAt(i);
            result += pow * (c - 'A' + 1);
            pow /= 26;
        }
        return result;
    }
View Code
原文地址:https://www.cnblogs.com/hf-cherish/p/4598879.html