将树节点:保存在数据库中是以id,parentid,name...方式存储的
现在要将上面的节点导成如下的EXCEL形式:请教高手指点一二,非常感谢...

解决方案 »

  1.   

    分析树将其解析到一个二维数组中,
    然后遍历这个二维数组存在excel中
    简单的话做成cvs文件每个cell用tab分隔不然就是poi hssf/xssf, 一个道理
      

  2.   

    以这种方式输出?
    http://hi.csdn.net/attachment/201109/30/51138_1317350083p8tC.jpg
      

  3.   

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        TreeModel tm = jTree1.getModel();
        Object root = tm.getRoot();
        Object[][] table = new Object[1][1];
        table = recursive(table, 0, tm, root);
        
        for (int i = table.length - 1; i >= 0; i--){
            for (int j = 0; j < table[i].length; j++){
                System.out.print(table[i][j]+" ");
            }
            System.out.println();
        }
    }private Object[][] recursive(Object[][] source, int level, TreeModel tm, Object currentObj){
        int count = tm.getChildCount(currentObj);
        if (count > 0){
            for (int i = count - 1; i >= 0 ; i--){
                source = recursive(source, level+1, tm, tm.getChild(currentObj, i));    
            }
        }
        int rowcount = source.length;
        int colcount = level+1;
        if (colcount > source[0].length){
            for (int i = 0; i< rowcount; i++){
                source[i] = Arrays.copyOf(source[i], colcount);
            }
        }
        source = Arrays.copyOf(source, ++rowcount);
        source[rowcount - 1] = new Object[colcount];
        source[rowcount - 1][colcount - 1] = currentObj;
       
        return source;
    }
      

  4.   

    给你个完整的
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {     //这个方法连到你的action里面                                     
        TreeModel tm = jTree1.getModel(); //获取jTree的model含有数据
        Object root = tm.getRoot(); //获取根节点
        Object[][] table = new Object[1][1];
        table = recursive(table, 0, tm, root);    HSSFWorkbook workbook = new HSSFWorkbook(); //创建xls workbook
        HSSFSheet sheet = workbook.createSheet("page1"); //创建sheet    int newrow = 0;
        for (int i = table.length - 1; i >= 0; i--) {
            HSSFRow row = sheet.createRow(newrow++);
            for (int j = 0; j < table[i].length; j++) {
                if (table[i][j] != null) {
                    row.createCell(j).setCellValue(table[i][j].toString()); //输出节点值到cell
                }
            }
        }
    /*输出到文件*/
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("E:\\test.xls");
            workbook.write(out);
        } 
        catch (FileNotFoundException ex) {ex.printStackTrace();}
        catch (IOException ex) {ex.printStackTrace();} 
        finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException ex) {ex.printStackTrace();}
        }
    }                                        
        private Object[][] recursive(Object[][] source, int level, TreeModel tm, Object currentObj) {
            int count = tm.getChildCount(currentObj); //获取下一级子节点数量
            if (count > 0) { //如果存在子节点嵌套加入数组中
                for (int i = count - 1; i >= 0; i--) {
                    source = recursive(source, level + 1, tm, tm.getChild(currentObj, i));
                }
            }
            int rowcount = source.length; //获取现有行数
            int colcount = level + 1; //获取现有列数也是tree中level数        source = Arrays.copyOf(source, ++rowcount); //拷贝原有数据并增加一新行
            source[rowcount - 1] = new Object[colcount]; //创建这一新行中数组
            source[rowcount - 1][colcount - 1] = currentObj; //赋值        return source;
        }
      

  5.   

    我将你的方法修改了一下,请高手指点
    private Object[][] recursive(Object[][] source, int level, List<ServiceLined> lineds, ServiceLined currentObj) {
    if (lineds!=null&&lineds.size()>0) {                //判断获取的节点集合是否为空
    for (int i = 1; i < lineds.size(); i++) {       //循环遍历节点集合
    ServiceLined lined = lineds.get(i);    //得到单个节点
    int count = mgr.findCountSubLined(lined);                                 //获取下一级子节点数量
            if (count > 0) {                                                                //如果存在子节点嵌套加入数组中
                for (int j = count - 1; j >= 0; j--) {
                    source = recursive(source, level + 1, lineds, lineds.get(j));
                }
            }
            int rowcount = source.length;                                                    //获取现有行数
            int colcount = level + 1;                                                        //获取现有列数也是tree中level数         source = Arrays.copyOf(source, ++rowcount);                                        //拷贝原有数据并增加一新行        
            source[rowcount - 1] = new Object[colcount];                                    //创建这一新行中数组
            source[rowcount - 1][colcount - 1] = currentObj;  //赋值
    }
    }
                                                   return source;
        }

    /**
     * 导出业务线下的模板信息
     * @return
     */
    public String exportServiceLine(){
    try {
    Integer parentId = serviceLineh.getId();
    List<ServiceLined> lineds = mgr.selectAllChild(parentId, true);   //得到模板1下的所有节点集合

    if(lineds!=null&&lineds.size()>0){        //判断获取的节点集合是否为空
    ServiceLined param = new ServiceLined();
    param.setParentid(parentId);
    param.setOther(Constant.IS_ROOT);
    ServiceLined rlined = mgr.findRootLined(param);                                                            //获取根节点
        Object[][] table = new Object[1][1];                        
        table = recursive(table, 0, lineds, rlined);     HSSFWorkbook workbook = new HSSFWorkbook();                                            //创建xls workbook
        HSSFSheet sheet = workbook.createSheet("page1");                                    //创建sheet     int newrow = 0;
        for (int i = table.length - 1; i >= 0; i--) {
            HSSFRow row = sheet.createRow(newrow++);
            for (int j = 0; j < table[i].length; j++) {
                if (table[i][j] != null) {
                    row.createCell((short)j).setCellValue(table[i][j].toString());                    //输出节点值到cell
                }
            }
        }
        /*输出到文件*/
        FileOutputStream out = new FileOutputStream("f://test.xls");
        workbook.write(out);
       
        out.close();
    }

    getResponse().setContentType("text/html;charset=utf-8");
    getResponse().getWriter().print("导出业务线模板信息成功!");
        
    } catch (Exception e) {
    logger.error(e);
    }

    return null;
    }
      

  6.   

    终于搞定了,将成果展示一下
    /**
     * 导出业务线下的模板信息
     * @return
     */
    public String exportServiceLine(){
    try {
    Integer parentId = serviceLineh.getId();
    List<ServiceLined> lineds = mgr.selectAllChild(parentId, true);

    List<ServiceLined> plineds=new ArrayList<ServiceLined>();
    List<ServiceLined> slineds=new ArrayList<ServiceLined>();
    List<ServiceLined> llineds=new ArrayList<ServiceLined>();

    if(lineds!=null&&lineds.size()>0){
    logger.debug("lineds**********************"+lineds.size());
    ServiceLined param = new ServiceLined();
    param.setParentid(parentId);
    param.setOther(Constant.IS_ROOT);
    ServiceLined rlined = mgr.findRootLined(param);
    logger.debug("rlined.id*****************************"+rlined.getId());

    for (int i = 1; i < lineds.size(); i++) {
    ServiceLined lined = lineds.get(i);
    logger.debug("equals**************"+lined.getParentid().equals(rlined.getId()));
    if (lined.getParentid().equals(rlined.getId())) {
    //int countSub = mgr.findCountSubLined(lined);
    plineds.add(lined);

    }
    }

    logger.debug("plineds**********************"+plineds.size());
    if (plineds.size()>0) {
    for (int i = 0; i < plineds.size(); i++) {
    for (int j = 1; j < lineds.size(); j++) {
    ServiceLined lined = lineds.get(j);
    if(plineds.get(i).getId().equals(lined.getParentid())){
    slineds.add(lined);
    }
    }
    }

    logger.debug("slineds**********************"+slineds.size());
    logger.debug("lineds**********************"+lineds.size());

    if(slineds.size()>0){
    for (int i = 0; i < slineds.size(); i++) {
    for (int j = 0; j < lineds.size(); j++) {
    ServiceLined lined = lineds.get(j);
    if (slineds.get(i).getId().equals(lined.getParentid())) {
    llineds.add(lined);
    }
    }
    }
    }

    logger.debug("llineds**********************"+llineds.size());
    }

    OutputStream os = new FileOutputStream(new File("f://test.xls"));
    WritableWorkbook book = Workbook.createWorkbook(os);
    WritableSheet sheet=book.createSheet("test", 0);
    sheet.setColumnView(0, 20);
    sheet.setColumnView(1, 20);
    sheet.setColumnView(2, 60);
    sheet.setRowView(0, 500);
    WritableCellFormat wcff= new WritableCellFormat();
    wcff.setWrap(true);
    wcff.setShrinkToFit(true);

    Label lableBuh=new Label(0,0,"父节点信息",wcff);
    Label lableBuh2=new Label(1,0,"子节点信息",wcff);
    Label lableBuh3=new Label(2,0,"叶节点信息",wcff);

    sheet.addCell(lableBuh);
    sheet.addCell(lableBuh2);
    sheet.addCell(lableBuh3);

    int count = 1;

    Label lableBu=new Label(0,count,"",wcff);
    Label lableBu2=new Label(1,count,"",wcff);
    Label lableBu3=new Label(2,count,"",wcff);

    boolean flag = false;
    if (plineds.size()>0) {
    for (int i = 0; i < plineds.size(); i++) {
    lableBu = new Label(0,count,plineds.get(i).getNode(),wcff);

    int countSubject = mgr.findCountSubLined(plineds.get(i));

    sheet.addCell(lableBu);
    if (countSubject>1) {
    sheet.mergeCells(0, count, 0, count+countSubject-1);
    }

    if(slineds.size()>0){
    for (int j = 0; j < slineds.size(); j++) {
    if (plineds.get(i).getId().equals(slineds.get(j).getParentid())) {
    lableBu2=new Label(1,count,slineds.get(j).getNode(),wcff);

    sheet.addCell(lableBu2);

    if(llineds.size()>0){
    String nodeName = "";
    for (int k = 0; k < llineds.size(); k++) {
    if (slineds.get(j).getId().equals(llineds.get(k).getParentid())) {
    nodeName += llineds.get(k).getNode()+"    ";

    int countSub = mgr.findCountSubLined(llineds.get(k));
    if (countSub>0) {
    for (int l = 0; l < lineds.size(); l++) {
    if (llineds.get(k).getId().equals(lineds.get(l).getParentid())) {
    nodeName += "\n  "+lineds.get(l).getNode();

    countSub = mgr.findCountSubLined(lineds.get(l));

    if (countSub>0) {
    for (int m = 0; m < lineds.size(); m++) {
    if (lineds.get(l).getId().equals(lineds.get(m).getParentid())) {
    nodeName += "\n    "+lineds.get(m).getNode()+"    ";
    }
    }
    }
    }
    }
    }


    lableBu3 = new Label(2,count,nodeName,wcff);
    sheet.addCell(lableBu3);

    //count++;

    }
    }

    count++;
    flag = true;
    }
    }


    if (!flag) {
    count++;
    }

    }
    }

    book.write();
    book.close();
    os.close();
        
        //getResponse().setContentType("text/html;charset=utf-8");
    //getResponse().getWriter().print("导出业务线模板信息成功!");
    }
        
    } catch (Exception e) {
    logger.error(e);
    }

    return null;
    }