Java 导出 Excel 列号数字与字母互相转换工具
2024-08-20 15:21:37
package test; /**
* Deal with Excel column indexToStr and strToIndex
* @author
* @version 2015-7-8
* @see
*/
public class ExcelColumn { public static void main(String[] args) {
String colstr = "AA";
int colIndex = excelColStrToNum(colstr, colstr.length());
System.out.println("'" + colstr + "' column index of " + colIndex); colIndex = 26;
colstr = excelColIndexToStr(colIndex);
System.out.println(colIndex + " column in excel of " + colstr); colstr = "AAAA";
colIndex = excelColStrToNum(colstr, colstr.length());
System.out.println("'" + colstr + "' column index of " + colIndex); colIndex = 466948;
colstr = excelColIndexToStr(colIndex);
System.out.println(colIndex + " column in excel of " + colstr);
} /**
* Excel column index begin 1
* @param colStr
* @param length
* @return
*/
public static int excelColStrToNum(String colStr, int length) {
int num = 0;
int result = 0;
for(int i = 0; i < length; i++) {
char ch = colStr.charAt(length - i - 1);
num = (int)(ch - 'A' + 1) ;
num *= Math.pow(26, i);
result += num;
}
return result;
} /**
* Excel column index begin 1
* @param columnIndex
* @return
*/
public static String excelColIndexToStr(int columnIndex) {
if (columnIndex <= 0) {
return null;
}
String columnStr = "";
columnIndex--;
do {
if (columnStr.length() > 0) {
columnIndex--;
}
columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;
columnIndex = (int) ((columnIndex - columnIndex % 26) / 26);
} while (columnIndex > 0);
return columnStr;
}
}
最新文章
- kindeditor-4.1.3工具使用技巧:如何在编辑区上传图片并保存绝对路径
- CSS3 text-shadow
- MySQL root密码找回
- SSH框架搭建详解 及 乱码处理
- Ubuntu14.04 LTS更新源
- Android网络框架源码分析一---Volley
- “合规性”是考核IT运维的重要指标
- 【C#|.NET】从细节出发(三) 逻辑层事务和page object模式
- chrome inspect 远程调测:Chrome on Android之一 普通调试
- centos7 docker1.12 私有仓库
- OpenSSL 安全漏洞: heartbleed
- [转载].Net中如何操作IIS(源代码)
- 使用JAVA与SmartFoxServer来实现游戏服务器概述
- 【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告
- Aa3.0 事件机制
- 使用Cain&;Abel进行网络嗅探
- Security+ 认证考过经验分享 802分飘过
- 在区块链上表白——使用C#将一句话放入比特币的区块链上
- React Native图片缓存解决方案
- js添加select中option
热门文章
- Oracle to_char(参数,&#39;FM990.00&#39;)函数
- Android--数据持久化之SQLite
- 关于tensorflow conv2d卷积备忘的一点理解
- JVM读书笔记之内存管理
- VS Code 安装 C++ 调试环境
- 【原创】驱动加载之OpenService
- Go语言的map如何判断key是否存在
- Maven json-lib依赖下载不下来解决方案
- Jenkins结合.net平台工具之Opencover
- Linux comm命令求出文件的交集、差集