本文實(shí)例講述了Java實(shí)現(xiàn)的Excel列號數(shù)字與字母互相轉(zhuǎn)換功能。分享給大家供大家參考,具體如下:

創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、大竹網(wǎng)絡(luò)推廣、成都小程序開發(fā)、大竹網(wǎng)絡(luò)營銷、大竹企業(yè)策劃、大竹品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供大竹建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:chinadenli.net
我們在實(shí)現(xiàn)對Excel的導(dǎo)入導(dǎo)出的時(shí)候,往往需要準(zhǔn)確的給用戶提示信息,提示到具體的Excel的單元格,這里就需要對Excel的列號進(jìn)行數(shù)字和字母的轉(zhuǎn)換,今天正好用到這個(gè)需求,所以就寫了一個(gè)demo,總結(jié)一下:
Java實(shí)現(xiàn):
package test;
/**
* Deal with Excel column indexToStr and strToIndex
* @author Stephen.Huang
* @version 2015-7-8
*/
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;
}
}
測試結(jié)果:
‘AA' column index of 27 26 column in excel of Z ‘AAAA' column index of 18279 466948 column in excel of ZNSN
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java操作Excel技巧總結(jié)》、《Java+MySQL數(shù)據(jù)庫程序設(shè)計(jì)總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java文件與目錄操作技巧匯總》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
文章題目:Java實(shí)現(xiàn)的Excel列號數(shù)字與字母互相轉(zhuǎn)換功能
鏈接地址:http://chinadenli.net/article12/pgpdgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站改版、、響應(yīng)式網(wǎng)站、App設(shè)計(jì)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)