博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Excel 列号数字与字母互相转换
阅读量:7084 次
发布时间:2019-06-28

本文共 2401 字,大约阅读时间需要 8 分钟。

我们在实现对Excel的导入导出的时候,往往需要准确的给用户提示信息,提示到具体的Excel的单元格,这里就需要对Excel的列号进行数字和字母的转换,今天正好用到这个需求,所以就写了一个demo,总结一下:

Java实现:

1 package test; 2  3 /** 4  * Deal with Excel column indexToStr and strToIndex 5  * @author Stephen.Huang 6  * @version 2015-7-8 7  * @see http://blog.csdn.net/u010571844 8  */ 9 public class ExcelColumn {10 11     public static void main(String[] args) {12         String colstr = "AA";13         int colIndex = excelColStrToNum(colstr, colstr.length());14         System.out.println("'" + colstr + "' column index of " + colIndex);15 16         colIndex = 26;17         colstr = excelColIndexToStr(colIndex);18         System.out.println(colIndex + " column in excel of " + colstr);19 20         colstr = "AAAA";21         colIndex = excelColStrToNum(colstr, colstr.length());22         System.out.println("'" + colstr + "' column index of " + colIndex);23 24         colIndex = 466948;25         colstr = excelColIndexToStr(colIndex);26         System.out.println(colIndex + " column in excel of " + colstr);27     }28 29     /**30      * Excel column index begin 131      * @param colStr32      * @param length33      * @return34      */35     public static int excelColStrToNum(String colStr, int length) {36         int num = 0;37         int result = 0;38         for(int i = 0; i < length; i++) {39             char ch = colStr.charAt(length - i - 1);40             num = (int)(ch - 'A' + 1) ;41             num *= Math.pow(26, i);42             result += num;43         }44         return result;45     }46 47     /**48      * Excel column index begin 149      * @param columnIndex50      * @return51      */52     public static String excelColIndexToStr(int columnIndex) {53         if (columnIndex <= 0) {54             return null;55         }56         String columnStr = "";57         columnIndex--;58         do {59             if (columnStr.length() > 0) {60                 columnIndex--;61             }62             columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;63             columnIndex = (int) ((columnIndex - columnIndex % 26) / 26);64         } while (columnIndex > 0);65         return columnStr;66     }67 }

测试结果:

‘AA’ column index of 27 26 column in excel of Z ‘AAAA’ column index of 18279 466948 column in excel of ZNSN

 参照来源【】:https://blog.csdn.net/u010571844/article/details/46806265

转载于:https://www.cnblogs.com/xianfengzhike/p/9417718.html

你可能感兴趣的文章
Windows Server2012上使用Nginx做文件服务器
查看>>
linux下的apache配置
查看>>
app测试和web端测试的区别
查看>>
一瓶汽水1元,两瓶汽水可换一瓶,现有20元,最多可喝多少瓶汽水
查看>>
图文教程自动登录expect脚本实例
查看>>
2019 第四周 开发笔记
查看>>
CORBA分布式实现
查看>>
第五天的学习
查看>>
微信小程序小技巧系列《二》show内容展示,上传文件编码问题
查看>>
Rancher Kubernetes Engine(RKE)正式发布:闪电般的Kubernetes安
查看>>
$.get()请求返回一个html页面,获取该页面特定id的元素
查看>>
Linux中的加解密实现
查看>>
Java开发GUI之CardLayout卡片布局
查看>>
将自己本地的项目托管到github上
查看>>
yum仓库搭建
查看>>
this怎么用(基础)
查看>>
安装server2008后,提示“Windows无法验证此文件的数字签名”
查看>>
一个中文空格引起的bug
查看>>
vmware用U盘安装win10
查看>>
CSS3内容溢出详解
查看>>