You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Julien Martin <ba...@gmail.com> on 2007/08/16 16:28:42 UTC

Damaged downloaded excel file using version 3.0.1 of POI

Hello,
I have a problem with a dowloaded excel file produced by POI. It says the
file is damaged...

Here is the method I use in order to download the file:

    public String exportEntireTable() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (!facesContext.getResponseComplete()) {
            String fileName = "export.xls";//todo

            ServletContext servletContext = (ServletContext)
            facesContext.getExternalContext().getContext();
            String contentType = servletContext.getMimeType (fileName);

            HttpServletResponse response = (HttpServletResponse)
            facesContext.getExternalContext().getResponse();
            response.setContentType(contentType);
            response.setHeader("Content-Disposition",
"attachment;filename=\"" + fileName + "\"");


            try {
                InputStream in = new
ByteArrayInputStream(retrieveEntireTable());//returns an array of bytes for
the file
                //BlockingInputStream bis = new BlockingInputStream(in);
                ServletOutputStream out = response.getOutputStream();

                byte[] buf = new byte[512];
                int bytesRead;
                while ((bytesRead = in.read(buf)) != -1) {
                    out.write(buf, 0, bytesRead);
                }

                out.flush();
                facesContext.responseComplete();
            } catch (IOException e) {
                log.fatal("IOException", e);
                throw new RuntimeException(e);
            }
        }
        return null;
    }

Can anyone please help?

Thanks in advance,

Julien.

Re: Damaged downloaded excel file using version 3.0.1 of POI

Posted by ap...@charter.net.
Is it true that the retrieveEntireTable method gets the data from the "table" and creates a HSSFWorkbook object?

If that is the case, then give something like this a try....

    private byte[] getBytesFromWorkbook(HSSFWorkbook workbook)
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try
        {
            workbook.write(bos);
            bos.close();
        } catch (IOException ex)
        {
            ex.printStackTrace();
            // Add error handling - probably want to throw this
        }
        return bos.toByteArray();
    }

You can probably get rid of everything except the call to workbook.write(bos).

I may be completely off base, but hopefully it will point you in the right direction.

---- Julien Martin <ba...@gmail.com> wrote: 
> Hello,
> I have a problem with a dowloaded excel file produced by POI. It says the
> file is damaged...
> 
> Here is the method I use in order to download the file:
> 
>     public String exportEntireTable() {
>         FacesContext facesContext = FacesContext.getCurrentInstance();
>         if (!facesContext.getResponseComplete()) {
>             String fileName = "export.xls";//todo
> 
>             ServletContext servletContext = (ServletContext)
>             facesContext.getExternalContext().getContext();
>             String contentType = servletContext.getMimeType (fileName);
> 
>             HttpServletResponse response = (HttpServletResponse)
>             facesContext.getExternalContext().getResponse();
>             response.setContentType(contentType);
>             response.setHeader("Content-Disposition",
> "attachment;filename=\"" + fileName + "\"");
> 
> 
>             try {
>                 InputStream in = new
> ByteArrayInputStream(retrieveEntireTable());//returns an array of bytes for
> the file
>                 //BlockingInputStream bis = new BlockingInputStream(in);
>                 ServletOutputStream out = response.getOutputStream();
> 
>                 byte[] buf = new byte[512];
>                 int bytesRead;
>                 while ((bytesRead = in.read(buf)) != -1) {
>                     out.write(buf, 0, bytesRead);
>                 }
> 
>                 out.flush();
>                 facesContext.responseComplete();
>             } catch (IOException e) {
>                 log.fatal("IOException", e);
>                 throw new RuntimeException(e);
>             }
>         }
>         return null;
>     }
> 
> Can anyone please help?
> 
> Thanks in advance,
> 
> Julien.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org