Thursday, 2 June 2016

Export functionality using ApachePOI in Oracle ADF/Webcenter

Hi ,
In ADF we have predefined set up to export table records in to excel(Using Export collection Action Listener).Problem with this approach is ,when we have more records(lacs of records),it will take more time to download excel(approximately 10 to 15 mins) ,but user can not wait this much of time.Solution for this problem is,by using ApachePOI we can export lacs of records within seconds.
steps:
------
Here I have implemented ApachePOI approach for Employee table in Hr schema.

1.Add File Download Action Listener to command button "Export" .


2.File Download Action Listener
   
      File Name : Example.xlsx

       Method    :#{ExportInApachePOI.exportAllToExcel}


3.Now go to your managed bean and create method in your class ExportInApachePOI.

       public void exportAllToExcel(FacesContext facesContext,
                                 OutputStream fileOut) {
        SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
        Sheet sheet = wb.createSheet();
        wb.setSheetName(0, "Employee");


        DCBindingContainer bc = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding iter = bc.findIteratorBinding("EmployeesView1Iterator");
        ViewObjectImpl viewObj = (ViewObjectImpl)iter.getViewObject();
        RowSetIterator rsi = iter.getRowSetIterator();
        String[] columHeaders={"EmployeeId","FirstName","LastName","Email","PhoneNumber","HireDate","JobId","Salary","CommissionPct","ManagerId","DepartmentId"};
        SXSSFRow xlHdrRow = (SXSSFRow)sheet.createRow(0);
        for (int j = 0; j < columHeaders.length; j++) {
            SXSSFCell cell = (SXSSFCell)xlHdrRow.createCell(j);
            String hdr = columHeaders[j];
            cell.setCellValue(hdr);
        }
        int rowNum = 1;
        rsi.first();
        ViewRowImpl row = (ViewRowImpl)rsi.getCurrentRow();
        while (row != null) {
            SXSSFRow xlRow = (SXSSFRow)sheet.createRow(rowNum++);
            for (int j = 0; j < columHeaders.length; j++) {

                SXSSFCell cell = (SXSSFCell)xlRow.createCell(j);
                Object attrVal = row.getAttribute(columHeaders[j]);
                if (attrVal != null) {
                    cell.setCellValue(attrVal.toString());
                } else {
                    cell.setCellValue("");
                }
            }
            row = (ViewRowImpl)rsi.next();
        }
        rsi.closeRowSetIterator();
        //write this workbook to an Outputstream.
        try {
            wb.write(fileOut);
            wb.dispose();
            fileOut.flush();
            fileOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Done .Happy working.

If u have any query on above ,contact me.Thanks.