Apache poi Excel:根据列的整数索引创建公式 [英] Apache poi Excel: Creating a formula based on the integer index of the column

查看:133
本文介绍了Apache poi Excel:根据列的整数索引创建公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的列号是26,当我创建一个公式时,我的公式应该是这样的,例如:SUM(AA1:AA3)。但是如何将26翻译成AA?或者说27到AB?有没有办法让Apache POI将Column索引用作整数并将其转换为字母表示形式?

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();
}

要测试的东西:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆