Java programming implementation of the EXCEL column name based on its index method

  • 2020-04-01 04:22:34
  • OfStack

The example of this article describes the method of Java programming implementation to find its index according to the EXCEL column name. Share with you for your reference, as follows:

Principle:
[a1-z26]*26^n-1 + [a1-z26]*26^n-2 +... + [a1 - z26] * ^ 0

The specific code is as follows:


 
import java.util.HashMap; 
import java.util.Map; 
 
public class ExcelUtil { 
  public static int getCellNum(String cellStr) { 
    char[] cellStrArray = cellStr.toUpperCase().toCharArray(); 
    int len = cellStrArray.length; 
    int n = 0; 
    for(int i=0;i<len;i++){ 
      n += (((int)cellStrArray[i])-65+1)*Math.pow(26, len-i-1); 
    } 
    return n-1; 
  } 
  public static void main(String[] args) { 
    System.out.print(getCellNum("aaa")); 
  } 
}

I hope this article has been helpful to you in Java programming.


Related articles: