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;
}
}

最新文章

  1. kindeditor-4.1.3工具使用技巧:如何在编辑区上传图片并保存绝对路径
  2. CSS3 text-shadow
  3. MySQL root密码找回
  4. SSH框架搭建详解 及 乱码处理
  5. Ubuntu14.04 LTS更新源
  6. Android网络框架源码分析一---Volley
  7. “合规性”是考核IT运维的重要指标
  8. 【C#|.NET】从细节出发(三) 逻辑层事务和page object模式
  9. chrome inspect 远程调测:Chrome on Android之一 普通调试
  10. centos7 docker1.12 私有仓库
  11. OpenSSL 安全漏洞: heartbleed
  12. [转载].Net中如何操作IIS(源代码)
  13. 使用JAVA与SmartFoxServer来实现游戏服务器概述
  14. 【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告
  15. Aa3.0 事件机制
  16. 使用Cain&amp;Abel进行网络嗅探
  17. Security+ 认证考过经验分享 802分飘过
  18. 在区块链上表白——使用C#将一句话放入比特币的区块链上
  19. React Native图片缓存解决方案
  20. js添加select中option

热门文章

  1. Oracle to_char(参数,&#39;FM990.00&#39;)函数
  2. Android--数据持久化之SQLite
  3. 关于tensorflow conv2d卷积备忘的一点理解
  4. JVM读书笔记之内存管理
  5. VS Code 安装 C++ 调试环境
  6. 【原创】驱动加载之OpenService
  7. Go语言的map如何判断key是否存在
  8. Maven json-lib依赖下载不下来解决方案
  9. Jenkins结合.net平台工具之Opencover
  10. Linux comm命令求出文件的交集、差集