Apache poi Excel:根据列的整数索引创建公式 [英] Apache poi Excel: Creating a formula based on the integer index of the column
本文介绍了Apache poi Excel:根据列的整数索引创建公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我的列号是 26,当我创建一个公式时,我的公式应该是这样的,例如:SUM(AA1:AA3).但我如何将 26 翻译成 AA?或者对AB说27?Apache POI 有没有办法将列索引用作整数并将其转换为字母表示?
Say that my column number is 26, when I create a formula my formula should look like, say: SUM(AA1:AA3) for example. but how do I translate 26 to AA? Or say 27 to AB? Is there a way for Apache POI to use Column index as an integer and translate it into its letter representation?
推荐答案
那只需要一点代码:
public static String columnName(int index) {
StringBuilder s = new StringBuilder();
while (index >= 26) {
s.insert(0, (char) ('A' + index % 26));
index = index / 26 - 1;
}
s.insert(0, (char) ('A' + index));
return s.toString();
}
还有一些东西可以测试:
And something to test it:
public static void main(String[] args) {
System.out.println(columnName(25));
System.out.println(columnName(26));
System.out.println(columnName(52));
System.out.println(columnName(27 * 26));
}
输出:
Z
AA
BA
AAA
这篇关于Apache poi Excel:根据列的整数索引创建公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文