将完整的树导出到 excel

Export full tree to excel

我正在尝试将完整的树导出到 Excel,但我遇到了问题。

我的树至少有 5 层,但我只能打印其中的两层。我不知道为什么它不会打印 children.

的其余部分

如果添加了新级别,我不想 re-write 代码。是否有任何递归解决方案来做到这一点? (主要是因为我的导出方法已经太大了)

导出方式:

public static void exportDataDrillDownParamComercial(FacesContext facesContext, OutputStream outputStream, RichTreeTable tree) throws IOException {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
CollectionModel treeModel = (CollectionModel) tree.getValue();
JUCtrlHierBinding treeBinding = (JUCtrlHierBinding) treeModel.getWrappedData();
List<JUCtrlHierNodeBinding> treeList = treeBinding.getChildren();
writer.write("Estrutura Comercial");writer.write(",");
writer.write("Estrutura Operacional Filtrada");writer.write(",");
writer.write("STK# Inicial (D-1)");writer.write(",");
writer.write("STK$ Inicial (D-1)"); writer.write(",");
writer.write("ROT Dias Inicial (D-1)");writer.write(",");
writer.write("MDQ% Vigor");writer.write(",");
writer.write("Presentation Stock");writer.write(",");
writer.write("MDQval Vigor");writer.write(",");
writer.write("VND ENA"); writer.write(",");
writer.write("VND REG U1S");writer.write(",");
writer.write("MDQval Novo");writer.write(",");
writer.write("ROT MDQ");writer.write(",");
writer.write("MDQ% Automático");writer.write(",");
writer.write("SS Vigor"); writer.write(",");
writer.write("FORECAST 2S");writer.write(",");
writer.write("VNDREG U2S");writer.write(",");
writer.write("IPSS% Automático");writer.write(",");
writer.write("SS Automático");writer.write(",");
writer.write("MDQ Rel. Automático");writer.write(",");
writer.write("MDQ% Min. Automático"); writer.write(",");
writer.write("Var. ROT MDQ");writer.write(",");
writer.write("Validade Var. ROT MDQ");writer.write(",");
writer.write("STK # Final");writer.write(",");
writer.write("STK $ Final");writer.write(",");
writer.write("ROT Dias Final");writer.write(",");
writer.write("MDQ% Final");writer.write(",");
writer.write("IPSS% Final");writer.write(",");
writer.write("SS Final");writer.write(",");
writer.write("MDQ Rel. Final");writer.write(",");
writer.write("MDQ% Mínimo Final");writer.write(",");            
writer.write("Data de Efetivação");writer.write(",");
writer.write("Var ROT Dias Automático");writer.write(",");
writer.write("Var STK # Automático");writer.write(",");
writer.write("Var STK # % Automático");writer.write(",");
writer.write("Var STK $ Automático");writer.write(",");
writer.write("Var STK $ % Automático");writer.write(",");
writer.write("Var MDQ% Automático");writer.write(",");
writer.write("Var ROT Dias Final");writer.write(",");
writer.write("Var STK # Final");writer.write(",");
writer.write("Var STK # % Final");writer.write(",");
writer.write("Var STK $ Final");writer.write(",");
writer.write("Var STK $ % Final");writer.write(",");
writer.write("Var MDQ% Final");writer.write(",");
writer.newLine();

for (int i = 0; i < treeList.size(); i++) {
    JUCtrlHierNodeBinding treeNode = treeList.get(i);
    Object[] treeAttrList = treeNode.getAttributes();
    for (int m = 0; m < treeAttrList.length ; m++) {
        Object attr = treeAttrList[m];
        if (attr instanceof java.lang.Long) {writer.write(((Long)attr).toString()); writer.write(";"); }
        if (attr instanceof java.lang.String) {writer.write(((String)attr)); writer.write(";"); }
    }
    writer.newLine();
    List<JUCtrlHierNodeBinding> subList = treeNode.getChildren();
    if(subList!=null) {
        for (int j = 0; j < subList.size(); j++) {
            JUCtrlHierNodeBinding subNode = subList.get(j);
            Object[] subAttrList = subNode.getAttributes();
            writer.write(";");writer.write(";");writer.write(";");writer.write(";");

            for (int n = 0; n < subAttrList.length ; n++) {
                Object subAttr = subAttrList[n];
                if(subAttr instanceof java.lang.Long) { writer.write(((Long)subAttr).toString()); writer.write(";"); }
                if(subAttr instanceof java.lang.String) { writer.write(((String)subAttr)); writer.write(";"); }
                if(subAttr instanceof oracle.jbo.domain.Date) { writer.write(((oracle.jbo.domain.Date)subAttr).stringValue()); writer.write(";"); }             
            }
            writer.newLine();               
        }
    }
    writer.newLine();
}
writer.flush();
}

您的问题是您的 header“,” 分隔开,但您的数据是“;”分开,在 treeList.size(); 循环之后你还有 newline();然后你循环遍历子列表,它给你另一行,但是这里你的代码的后半部分被修改了,试试吧,

for (int i = 0; i < treeList.size(); i++) {
    JUCtrlHierNodeBinding treeNode = treeList.get(i);
    Object[] treeAttrList = treeNode.getAttributes();
    for (int m = 0; m < treeAttrList.length ; m++) {
        Object attr = treeAttrList[m];
        if (attr instanceof java.lang.Long) {writer.write(((Long)attr).toString()); writer.write(","); }
        if (attr instanceof java.lang.String) {writer.write(((String)attr)); writer.write(","); }
    }
    //writer.newLine();
    List<JUCtrlHierNodeBinding> subList = treeNode.getChildren();
    if(subList!=null) {
        for (int j = 0; j < subList.size(); j++) {
            JUCtrlHierNodeBinding subNode = subList.get(j);
            Object[] subAttrList = subNode.getAttributes();
            writer.write(",");writer.write(",");writer.write(",");writer.write(",");

            for (int n = 0; n < subAttrList.length ; n++) {
                Object subAttr = subAttrList[n];
                if(subAttr instanceof java.lang.Long) { writer.write(((Long)subAttr).toString()); writer.write(","); }
                if(subAttr instanceof java.lang.String) { writer.write(((String)subAttr)); writer.write(","); }
                if(subAttr instanceof oracle.jbo.domain.Date) { writer.write(((oracle.jbo.domain.Date)subAttr).stringValue()); writer.write(","); }             
            }
            writer.newLine();               
        }
    }
    writer.newLine();
}
writer.flush();
}