Monday, August 13, 2012

ADF 11G: How to Convert the Data of an ADF Table into Excel-sheet?

 Use the below code:

Approach 1:

  af:exportCollectionActionListener     exportedId="t1"
                                                       type="excelHTML"/>

Note: exportedId is the Id of the ADF RichTable;
*************************************************************
Approach 2:

 public String exportToExcel() {

        RichTable ct = this.getT1();
        FacesContext fctx= null;
               
                PrintWriter out = null;
                try {
                    ExternalContext ec =
                        FacesContext.getCurrentInstance().getExternalContext();
                    HttpServletResponse hsr = (HttpServletResponse)ec.getResponse();
                    hsr.setHeader("Content-disposition", "attachment; filename=a.csv");
                    hsr.setContentType("application/vnd.ms-excel");
                    out = hsr.getWriter();
               

                    List l = ct.getChildren();
                    ArrayList colList = new ArrayList();
                    ArrayList rowData = new ArrayList();
                    String tempColHdr = "";
                    for (int i = 0; i < l.size(); i++) {
                        l.get(i);
                        RichColumn cc = (RichColumn)l.get(i);

                        RichOutputText cit = (RichOutputText)(cc.getChildren().get(0));
                     
                        ValueExpression ve= cit.getValueExpression("value");
                     
                        String es = ve.getExpressionString();
                     
                        String tempStr =  es.substring(6, es.length() - 1);
              
                        tempColHdr = tempColHdr + cc.getHeaderText();
                        colList.add(tempStr);
                        if (i != l.size() - 1)
                            tempColHdr = tempColHdr + ",";
                    }
              
                    out.print(tempColHdr);
                    out.println();
                    int rowCount = ct.getRowCount();
                    for (int i = 0; i < rowCount; i++) {
                        FacesCtrlHierNodeBinding a =
                            (FacesCtrlHierNodeBinding)ct.getRowData(i);
                        Row r = a.getRow();
                        String tempData = "";
                        for (int j = 0; j < l.size(); j++) {
                            Object colValue =
                                r.getAttribute(colList.get(j).toString());
                            if (colValue == null)
                                colValue = "";
                            tempData = tempData + colValue;
                            if (j != l.size() - 1)
                                tempData = tempData + ",";
                        }
                        out.print(tempData);
                        out.println();
                        rowData.add(tempData);
                    }
                    FacesContext.getCurrentInstance().responseComplete();
                } catch (Exception e) {
               e.printStackTrace();
               }finally{
                    this.getT1().setRendered(true);
                    }
        return null;
    }