首页 > 开发 > Java > 正文

java编程实现根据EXCEL列名求其索引的方法

2024-07-13 09:56:08
字体:
来源:转载
供稿:网友

这篇文章主要介绍了java编程实现根据EXCEL列名求其索引的方法,涉及Java元素遍历与数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了java编程实现根据EXCEL列名求其索引的方法。分享给大家供大家参考,具体如下:

原理:

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

具体代码如下:

 

 
  1. /*  
  2. * To change this template, choose Tools | Templates  
  3. * and open the template in the editor.  
  4. */  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7. /**  
  8.  
  9. * @author jdkleo  
  10. */ 
  11. public class ExcelUtil {  
  12. public static int getCellNum(String cellStr) {  
  13. char[] cellStrArray = cellStr.toUpperCase().toCharArray();  
  14. int len = cellStrArray.length;  
  15. int n = 0;  
  16. for(int i=0;i<len;i++){  
  17. n += (((int)cellStrArray[i])-65+1)*Math.pow(26, len-i-1);  
  18. }  
  19. return n-1;  
  20. }  
  21. public static void main(String[] args) {  
  22. System.out.print(getCellNum("aaa"));  
  23. }  

希望本文所述对大家java程序设计有所帮助。


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表