0

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Priya
  • 1
  • 1
  • You can increase your chances of getting help by editing your question to contain a [mcve]. – Selaron Nov 18 '19 at 15:24
  • Thanks Selaron, done – Priya Nov 18 '19 at 15:28
  • 1
    Just turn off ajax as indicated in the duplicate. – BalusC Nov 18 '19 at 16:15
  • I am not able to turn off ajax.Below is the error message. SEVERE: Servlet.service() for servlet Persistent Faces Servlet threw exception com.sun.facelets.tag.TagException: /jsp/CheckPointReport.jspx @251,40 Tag Library supports namespace: http://java.sun.com/jsf/core, but no tag was defined for name: ajax – Priya Nov 19 '19 at 14:34
  • Code as below – Priya Nov 19 '19 at 14:35
  • Hi BalusC Just turn off ajax as indicated in the duplicate . I am using jsf 1.8. this version is not compatible to turn off the ajax. So is showing error. I tried with jar file ajax4jst1.0.6.jar still not accepting. Please help – Priya Nov 19 '19 at 16:39

0 Answers0