将完整树导出为ex​​cel

时间:2015-03-05 14:59:36

标签: java export-to-excel oracle-adf

我试图将完整的树导出到Excel并且我遇到了问题。

  • 第一:

我的树至少有5个级别,但我只能打印其中的两个级别。我不知道为什么它不能打印其他孩子。

  • 第二

如果添加了新级别,我就不会重新编写代码。有没有任何递归解决方案呢? (主要是因为我的导出方法已经太大了)

导出方法:

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

1 个答案:

答案 0 :(得分:0)

你的问题是你有标题&#34;,&#34;已分开,但您的数据是&#34;;&#34;在循环使用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();
}