4 Replies Latest reply on Feb 5, 2008 12:16 AM by abcd1

    Is it possible to export the data to an excel document?

    abcd1

      hii everyone

      Is it possible to export the data to an excel document?
      anybody know the solution

      thanks in advance
      cheers

        • 1. Re: Is it possible to export the data to an excel document?

          You can always use the apache POI library. I use it to export data from tables to excel. Let me know if you need me to post code snipets or anything.

          http://poi.apache.org/

          • 2. Re: Is it possible to export the data to an excel document?
            abcd1

            Thanks for the reply..
            it will be very helpful if u could provide me some sample code.

            • 3. Re: Is it possible to export the data to an excel document?
              dexjam

              Hi, you may want to use s.th. like this (reads all fields from your entity,
              you may want to change that to your specific fields)

              public void exportToExcel() {
              
              
              
               ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
               HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
               OutputStream out;
               try {
               out = response.getOutputStream();
               response.setContentType("application/vnd.ms-excel");
               response.setHeader("Content-disposition", "attachement; filename=EntityList.xls");
               HSSFWorkbook workbook = new HSSFWorkbook();
               HSSFSheet sheet = workbook.createSheet("Entity results");
              
              
               Class clazz = Class.forName("org.example.Entity");
               Field fieldList[] = clazz.getDeclaredFields();
              
               Iterator it = allClients.iterator();
               short columnCounter = 0;
               short rowCount = 0;
               while (it.hasNext()) {
               Entity s = (Entity) it.next();
              
               HSSFRow row = sheet.createRow(rowCount++);
               for (short i = 0; i < fieldList.length; i++) {
               row.createCell(i).setCellValue(BeanUtils.getProperty(s, fieldList.getName()));
               }
               }
               workbook.write(out);
               out.flush();
               out.close();
               FacesContext.getCurrentInstance().responseComplete();
               } catch (SecurityException ex) {
               ex.printStackTrace();
               } catch (IllegalArgumentException ex) {
               ex.printStackTrace();
               } catch (InvocationTargetException ex) {
               ex.printStackTrace();
               } catch (ClassNotFoundException ex) {
               ex.printStackTrace();
               } catch (IOException ex) {
               ex.printStackTrace();
               } catch (IllegalAccessException ex) {
               ex.printStackTrace();
               } catch (NoSuchMethodException ex) {
               ex.printStackTrace();
               }
               }



              allClients is:
              @DataModel
               private List<Entity> allClients;


              Cheers,
              Jens

              • 4. Re: Is it possible to export the data to an excel document?
                abcd1

                thanks for the reply. i am sorry i forgot to tell that i need to export the data from a rich:dataTable to excel sheet.
                can i make use of the same code

                thanks in advance