Jxls或Jett:如何在父节点上渲染具有公式的树?

时间:2017-03-21 19:55:11

标签: java excel jxls jett

有没有办法使用JXLS(或JETT)生成分层树的报告?例如,模型类似于:

-node1
--node11
--node12
---node121
---node1211
--node13
-node2

我需要用两列来渲染结果:

| node name | node value
| node1     | =sum(node11, node12, node13)
| node11    | 5
| node12    | =sum(node121)
| node121   | =sum(node1211)
| node1211  | 10
| node13    | 15
| node2     | 20

....

我不知道树中嵌套级别的数量。我的问题是,对于每个父节点,excel中的渲染值必须是具有直接子节点之和的公式......依此类推。只有叶节点必须使用实际节点值进行渲染。

谢谢

1 个答案:

答案 0 :(得分:0)

从这里获取代码: Traversing a tree recursively in depth first problems

public void traverseTree(Tree tree) {

    // print, increment counter, whatever
    System.out.println(tree.toString());
    // Do your code to build your sum formula string here and store it on your tree node.

    // traverse children
    int childCount = tree.getChildCount();
    if (childCount == 0) {
        // Overwrite the "forumla" you were building with something denoting that it is a leaf node here.
    } else {
        for (int i = 0; i < childCount; i++) {
            Tree child = tree.getChild(i);
            traverseTree(child);
        }
    }
}

在节点上存储构建的公式后,再次遍历它,但这次代替System.out.println(tree.toString()); line - 实际上是使用JXLS API将其写入Excel。

(请注意,代替“Tree”对象,您可能需要定义自己的Tree类来包装Tree节点,并允许您存储公式信息。)