Flipkart Search

Monday 31 March 2014

Custom Export to Excel in functionality in Oracle ADF

    public static void exportHtmlTableToExcel(DCIteratorBinding customVO1Iterator) throws IOException {

                String filename = "ExportedExcel.csv";

        customVO1Iterator.setRangeSize(4000);

        //Setup the output

        String contentType = "application/vnd.ms-excel";
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpServletResponse response =
            (HttpServletResponse)fc.getExternalContext().getResponse();
        response.setHeader("Content-disposition",
                           "attachment; filename=" + filename);

        response.setContentType(contentType);
        PrintWriter out = response.getWriter();
        RowSetIterator rsi = customVO1Iterator.getRowSetIterator();
        //    Get the Headers
        String[] attNames = rsi.getRowAtRangeIndex(0).getAttributeNames();

        for (int i = 0; i < attNames.length; i++) {
            if (i > 0)
                out.print(",");
            out.print(attNames[i]);

        }
        out.println();
//Setting the first row data ,was creating problem in my application-- so called reset
                rsi.reset();
        Boolean isFirst = true;
        while (rsi.hasNext()) {
            Row currentRow = null;
            if (isFirst) {
                currentRow = rsi.first();
                isFirst = false;
            } else {
                currentRow = rsi.next();
            }
            if (currentRow != null) {
                Object[] attValues = currentRow.getAttributeValues();
                for (int j = 0; j < attValues.length; j++) {
                    if (j > 0)
                        out.print(",");
                    if (attValues[j] != null)
                        out.print("\"" + attValues[j] + "\"");
                }

            }
            out.println();
        }
        out.close();

        fc.responseComplete();

        customVO1Iterator.setRangeSize(30);

    }

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete