How to download excel file using JSF and Java

48 views Asked by At

I am using JSF and Java , I googled and tried many different ways to download generated excel in client browser. Can not able to find where I am wrong.I tried most of the solution from stakoverfow . Still can not able download the generated excel file

In the below code deliveryPointsReportCtrl my bean and retrieveTrip function.

public void retrieveTrip(ActionEvent event) {

        System.out.println("retrieveTrip");
        export();
    }



  public void printExcel(ResultSet rs)
        {
            try {     
                HSSFWorkbook workbook = new HSSFWorkbook();
                HSSFSheet sheet = workbook.createSheet("TripDetails");          
                HSSFRow rowhead = sheet.createRow((short) 0);           
                HSSFCellStyle style = workbook.createCellStyle();         
                List<String> header = getColumnNames(rs);  short i=0;
                sheet.createFreezePane(0, 1);          

                for(String headCell : header){
                    rowhead.createCell((short) i).setCellValue(headCell);           
                    rowhead.setRowStyle(style);         
                    i++;
                }

                int k = 3;
                while (rs.next()){

                    HSSFRow row = sheet.createRow((short) k);
                    row.createCell((short) 0).setCellValue(rs.getString("name"));


                    k++;
                }
                    String fileName  = "tripdetails.xls" ;
                    HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();           
                    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
                    response.setContentType("application/vnd.ms-excel");
                    response.setHeader("Content-disposition", "inline;filename=inline.xls");


                    FileOutputStream fileOut0 = new FileOutputStream(fileName);   
                    workbook.write(fileOut0);    
                    workbook.write(response.getOutputStream()); // Write workbook to response.
                    response.getOutputStream().flush();    
                    fileOut0.close();    
                    response.getOutputStream().close();

                    } catch (final Exception e) {
                        System.out.println("-------------------error----------------------");
                    }

        }

UI Part

<td colspan="9"></td>
                                            <td style="width: 200px"><ice:commandButton
                                                    id="cmdRetrieve" value="#{bundle.Label_Retrieve}"
                                                    actionListener="#{deliveryPointsReportCtrl.retrieveTrip}"
                                                    disabled="#{deliveryPointsReportCtrl.disablePrintField}"
                                                    style="width:120px;" visible="true" /> 

                                                    <ice:message
                                                    for="cmdRetrieve" 
                                                     /></td>

                                            <td>
0

There are 0 answers