やりたいこと
10進数を26進数(アルファベット)に変換したい
Excelとかの列番号文字列を得たいので
(A, B, C, ..., X, Y, Z, AA, AB, AC, ...)
この文字列は以下のように呼ぶらしい
https://en.wikipedia.org/wiki/Bijective_numeration#The_bijective_base-26_system
やったこと
https://gist.github.com/theazureshadow/4a5a032944f1c9bc0f4a をJavaにした
public class Bijective {
static String base26(int n) {
StringBuilder buf = new StringBuilder();
while (n != 0) {
buf.append((char) ((n - 1) % 26 + 'A'));
n = (n - 1) / 26;
}
return buf.reverse().toString();
}
public static void main(String[] args) {
for (int n = 1; n <= 100; n++) {
System.out.printf("%d - %s%n", n, base26(n));
}
}
}
編集リクエストいただいた内容(自分では試してない)
POI のCellReferenceでエクセルの列やセルの行列番号文字列を取得できる
CellReference ref = new CellReference("sheet1", 0, 0, false, false);
System.out.println(ref.formatAsString());
結果:sheet!A1
例1:new CellReference(2, 0).formatAsString()
⇒ A3
例2:new CellReference(2, 0, true, true).formatAsString()
⇒ $A$3
例3:new CellReference("sheet", 2, 0, false, true).formatAsString()
⇒ sheet!$A3