您的位置:首页 > 编程语言 > Java开发

Java通过列索引获取Excel其对应列的字母(两种实现算法)

2012-11-08 13:55 435 查看
第一种:

public static String getExcelColumnLabel(int num) {
String temp = "";
double i = Math.floor(Math.log(25.0 * (num) / 26.0 + 1) / Math.log(26)) + 1;
if (i > 1) {
double sub = num - 26 * (Math.pow(26, i - 1) - 1) / 25;
for (double j = i; j > 0; j--) {
temp = temp + (char) (sub / Math.pow(26, j - 1) + 65);
sub = sub % Math.pow(26, j - 1);
}
} else {
temp = temp + (char) (num + 65);
}
return temp;
}


第二种

public static String getExcelColumnLabel(int iCol) {
String strCol = "";
int baseCol = 65 + iCol;
if (baseCol > 90) {
// 十位位置
int i2 = 0;
if ((baseCol - 90) / 26 > 0) {
i2 = 65 + ((baseCol - 90 - 1) / 26);
} else {
i2 = 65;
}
// 个位位置
int i1 = ((baseCol - 90 - 1) % 26);
i1 = 65 + i1;

strCol = String.valueOf((char) i2) + String.valueOf((char) i1);
} else {
strCol = String.valueOf((char) baseCol);
}
return strCol;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐