java导出excel为数字型_Java 导出 Excel 列号数字与字母互相转换工具

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值