一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java實現(xiàn)的Excel列號數(shù)字與字母互相轉(zhuǎn)換功能

Java實現(xiàn)的Excel列號數(shù)字與字母互相轉(zhuǎn)換功能

2021-04-12 09:09Stephen102 Java教程

這篇文章主要介紹了Java實現(xiàn)的Excel列號數(shù)字與字母互相轉(zhuǎn)換功能,涉及java針對Excel相關(guān)數(shù)值與字符串操作技巧,需要的朋友可以參考下

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

我們在實現(xiàn)對Excel的導(dǎo)入導(dǎo)出的時候,往往需要準(zhǔn)確的給用戶提示信息,提示到具體的Excel的單元格,這里就需要對Excel的列號進(jìn)行數(shù)字和字母的轉(zhuǎn)換,今天正好用到這個需求,所以就寫了一個demo,總結(jié)一下:

Java實現(xiàn):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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é)果:

?
1
2
3
4
‘AA' column index of 27
26 column in excel of Z
‘AAAA' column index of 18279
466948 column in excel of ZNSN

希望本文所述對大家java程序設(shè)計有所幫助。

原文鏈接:http://blog.csdn.net/u010571844/article/details/46806265

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 嫩草影院精品视频在线观看 | 娇小性色 | 亚洲国产精品牛在线 | 513热点网 | 国产香蕉一区二区在线网站 | 99re8在这里只有精品2 | 幻女free性zozo交体内谢 | 日韩视频一 | les女同h高h喷水 | bnb998八度免费影院丫意浓 | 福利色播 | 成人国产在线视频在线观看 | 亚洲国产第一区二区三区 | 美国美女hd18 | 青春草在线观看视频 | 俄罗斯女人与公拘i交酡 | 亚洲图片二区 | 国产高清日韩 | 成人黄色a级片 | 日本暖暖在线视频 | 无限资源在线观看高清 | 国产网站免费在线观看 | 日本漫画工囗全彩内番e绅 日本伦理动漫在线观看 | youzljzljzljzlj96| 香蕉久久一区二区不卡无毒影院 | 日本在线小视频 | 四虎影院网址大全 | 日日碰日日操 | 婷婷丁香视频 | 91网红福利精品区一区二 | 国产一区二区三区免费在线视频 | 天选之王漫画顾长歌免费阅读 | 国产视频一区二区 | 娇小老少配xxxxx性视频 | 免费永久观看美女视频网站网址 | 男女全黄h全肉细节文 | 午夜精品久久久久久久99蜜桃 | 亚欧有色在线观看免费版高清 | 桃色视频软件 | 超级碰碰青草免费视频92 | 精品香蕉99久久久久网站 |