1 Reply Latest reply on Jan 26, 2007 9:01 PM by 843842

    output ArrayList to excel in jsf

    843842
      Hi,

      I want to show an arraylist (or a string with data if arraylist is not possible) in excel format within my jsf page, but i don't want to save the data to a file first... is this possible?

      I've tried to do the following, but the data is not what i wanted...
        public String generateExcelOutput(List list){
          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
          ObjectOutputStream objectOutputStream;
          
          try {
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(list);
          } catch (IOException e) {
             throw new FacesException(e);
          }
          
          byte[] excelData = byteArrayOutputStream.toByteArray();
          FacesContext faces = FacesContext.getCurrentInstance();
          HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
          response.setContentType("application/vnd.ms-excel");
          response.setContentLength(excelData.length);
          
          try {
            ServletOutputStream out;
            out = response.getOutputStream();
            out.write(excelData);
          } catch (IOException e) {
            throw new FacesException(e);
          }
          faces.responseComplete();
          
          return null;
        }
      --> a popup disappears with the question to save or open...that's correct but i also get a notice that the file is corrupt and the data is not shown as it should be...

      Can anyone help me, perhaps with an example...
      Thanks!
        • 1. Re: output ArrayList to excel in jsf
          843842
          By using jakarta-poi, output the workbook like this...
           
          HSSFWorkbook generatedExcel = null;
          generatedExcel = generateExcel(blah blah);
          generatedExcel.write(response.getOutputStream());
          If you are trying to export your datatables to excel, use excelcreator

          http://sourceforge.net/project/showfiles.php?group_id=137466&package_id=188186