views:

1135

answers:

2

Hello,

I'm using JSF/ICEFaces. The page is ICEFaces however I'm using the JSF datatable because ICEFaces had slow performance for some reason. Anyway, unlike the ICEFaces datatable, the JSF table doesn't come with export to excel so I'm writing my own. I decided to use Apache POI as below. The code executes well but I don't see pop up to save excel file. Am I missing something?

    public void ExportWithPoi(ActionEvent e) throws IOException{
         HSSFWorkbook wb = new HSSFWorkbook();
         HSSFSheet sheet = wb.createSheet();
//       ArrayList<PerfStatBean> statFilterResults;
         Iterator<PerfStatBean> statsIterator = statFilterResults.iterator();
         int i=0;
                 HSSFRow row;
                 row = sheet.createRow((short)0);
                 row.createCell((short)0).setCellValue("Current Application ID");
                 row.createCell((short)1).setCellValue("Event Name");
                 row.createCell((short)2).setCellValue("Generic Method Name");         
                 while(statsIterator.hasNext()){
            i++;                   
            row = sheet.createRow((short)i);   
            PerfStatBean perfBean = statsIterator.next();
            row.createCell((short)0).setCellValue(perfBean.getCurrent_appl_id());
            row.createCell((short)1).setCellValue(perfBean.getCurrent_appl_id());
            row.createCell((short)2).setCellValue(perfBean.getGeneric_method_name());

        }
                 HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
                 res.setContentType("application/vnd.ms-excel");
                 res.setHeader("Content-disposition",  "attachment; filename=PerfCollector.xls");


                 try {
                     ServletOutputStream out = res.getOutputStream();

                      wb.write(out);
                     out.flush();
                      out.close();
                } catch (IOException ex) {
                        ex.printStackTrace();
                }

               FacesContext faces = FacesContext.getCurrentInstance();
               faces.responseComplete();
    }

And the excel button is:

   <h:commandButton id="excelBtn"
      rendered="#{statsDisplayAndFilter.renderNextBtn}"
                image="./xmlhttp/css/rime/css-images/excel.png"
                actionListener="#{statsDisplayAndFilter.ExportWithPoi}"/>

Thanks,

Tam

A: 

I'd suggest you pull this code into its own servlet. You're trying to short circuit JSF's usual life-cycle and while you might be able to find a way to do it, it would probably be simpler and possibly even cleaner to simply make a new servlet which handles this XLS file and let it do its own thing. You can place the appropriate objects into session temporarily from your action button, direct them to the servlet, grab them in the servlet and then remove them from session.

Drew
That seems to be good choice. Could you please give me a code or link on how to handle that? With my code above I could see with FireBug I can see the response body contains the Excel file and the content type is application/vnd.ms-excel but it doesn't pop it up for download. I believe if I have it as separate servlet and forward to that servlet it might do the job. Thanks!
Tam
This seems to have most of what you need and/or has links to what you need: http://www.devx.com/Java/Article/31356
Drew
A: 

I have posted a blog post specifically for an ICEFaces implementation here.

KG