Thursday, May 31, 2012

Exporting master-child data to CSV or Excel from a tree table


If you want to export the data from a treeTable that shows the master detail hierarchy, we can't use the af:exportCollectionActionListener. It exports only the master records data not the child record's data.

To export both master and child records data, I used af:fileDownloadActionListener to export/download the master-detail data in a CSV file. We can also export the same data to an excel file by using apache POI apis.

Steps followed:

1) Create the master-detail data model (Departments - Employee) in the application module.
2) Drag the DepartmentVO as the adf tree table onto a jspx page.
3) Create a command button (Export) and drop the component af:fileDownloadActionListener inside
    the Export button.




4) Write the below logic inside the bean method to add the data to the file.



5) When you run the page, click on the Export button. It will export the Departments-Employees data in the
     hierarchical way.









To export the data to excel instead of CSV, create another  button in the jspx as

      <af:commandButton text="ExportXLS" id="cb3" immediate="true">
          <af:fileDownloadActionListener filename="EmpDept.xls"
                                         method="#{backingBeanScope.TreeBean.exportDataToExcel}"
                                         contentType="application/vnd.ms-excel"/>
        </af:commandButton>


    private void addLabel(WritableSheet sheet, int column, int row, String s)
        throws WriteException, RowsExceededException {
      Label label;     
      label = new Label(column, row, s, timesBoldUnderline);
      sheet.addCell(label);
    }


 To export the data to the xls file, use below code inside your bean



No comments:

Post a Comment