You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Brett Procek <b....@yahoo.com> on 2006/10/17 23:58:40 UTC

Re: Mail attachment

Anil Komareddi <anil <at> effone.net> writes:

> 
> 
> I'm trying to generate an excel spreadsheet and then email it as an
> attachment. The generation part works fine. I can save this to a file
> and view it. I can attach an existing spreadsheet file to an email and
> send it and it goes out ok. The problem is when I try to generate the
> spreadsheet and attach it without saving it first.
> 
> This is what I'm doing:
> 
> 1. Generate the workbook
> 2. Get the data byte array from the workbook using
> HSSFWorkbook.getBytes()
> 3. Prepare a javax.activation.Datasource using this byte array where the
> getInputStream method is as follows:
> 
>     public InputStream getInputStream()
>                                throws IOException
>     {
>         if (_data == null)
>         {
>             throw new IOException("no data");
>         }
> 
>         return new ByteArrayInputStream(_data);
>     }
> 
> 4. The emailer code itself, looks like this:
> 
> "Lost Document Summary Information"
> 

I had this same issue and figured out the problem.  Invoking 
HSSFWorkbook.getBytes() does not return all of the data necessary to re-
construct a complete Excel file. An excel file itself contains more than the 
raw contents of the file.  The 'getBytes()' method apparently seems to miss 
other excell file meta data, including the file summary information, etc.

Instead, in order to write the file out as an attachment, or stream, you must 
invoke the 'write()' method on the workbook itself, passing in a OutputSteam.

- Brett




---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


RE: Mail attachment

Posted by Tahir Akhtar <ta...@spectrum-tech.com>.
Adriana,
It is tough to say anything about your problem without looking at the
exception stack trace.
For null pointer exception, it is crucial to know which reference was null
(and this information can only be guessed by stacktrace)

Regards,
Tahir

> -----Original Message-----
> From: Adriana Rozo [mailto:adrianis@gmail.com]
> Sent: Friday, May 11, 2007 10:06 PM
> To: POI Users List
> Subject: Re: Mail attachment
> 
> Hi all,
> 
> I have the same problem,
> this is my code snippet
> 
> //HttpServletResponse
> getResponse().setContentType("application/vnd.ms-excel");
> getResponse().setHeader("Content-disposition","attachment; filename=
> "+theNameFile+".xls" );
> getResponse().setHeader("Cache-Control"," private");
> 
> OutputStream unStream = null;
> try {
>   unStream = getResponse().getOutputStream();
>   workbook.write(unStream); //IT CRASHES
>   unStream.flush();
>   unStream.close();
>  ...
> 
> -->> I'm using poi-3.0-alpha1-20050704
> But I obtain a NullPointerException
> 
> I tryed:
> 
> ByteArrayOutputStream byteOutS = new ByteArrayOutputStream();
> wb.write(byteOutS);//IT CRASHES
> byte[] excelBytes=byteOutS.toByteArray();
> 
> And once again NullPointerException
> 
> does it work only with FileOutputStream?
> 
> thanks
> --
> Adriana
> p.d. sorry for my english... (ó_ò)
> 
> On 10/17/06, Brett Procek <b....@yahoo.com> wrote:
> >
> > Anil Komareddi <anil <at> effone.net> writes:
> >
> > >
> > >
> > > I'm trying to generate an excel spreadsheet and then email it as an
> > > attachment. The generation part works fine. I can save this to a file
> > > and view it. I can attach an existing spreadsheet file to an email and
> > > send it and it goes out ok. The problem is when I try to generate the
> > > spreadsheet and attach it without saving it first.
> > >
> > > This is what I'm doing:
> > >
> > > 1. Generate the workbook
> > > 2. Get the data byte array from the workbook using
> > > HSSFWorkbook.getBytes()
> > > 3. Prepare a javax.activation.Datasource using this byte array where
> the
> > > getInputStream method is as follows:
> > >
> > >     public InputStream getInputStream()
> > >                                throws IOException
> > >     {
> > >         if (_data == null)
> > >         {
> > >             throw new IOException("no data");
> > >         }
> > >
> > >         return new ByteArrayInputStream(_data);
> > >     }
> > >
> > > 4. The emailer code itself, looks like this:
> > >
> > > "Lost Document Summary Information"
> > >
> >
> > I had this same issue and figured out the problem.  Invoking
> > HSSFWorkbook.getBytes() does not return all of the data necessary to re-
> > construct a complete Excel file. An excel file itself contains more than
> > the
> > raw contents of the file.  The 'getBytes()' method apparently seems to
> > miss
> > other excell file meta data, including the file summary information,
> etc.
> >
> > Instead, in order to write the file out as an attachment, or stream, you
> > must
> > invoke the 'write()' method on the workbook itself, passing in a
> > OutputSteam.
> >
> > - Brett
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> > Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> > The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
> >
> >
> 
> 
> --
> Adriana
> --.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.
> Your grace has found me just as I am,
> empty handed but alive in Your hands



---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: Mail attachment

Posted by Adriana Rozo <ad...@gmail.com>.
Hi all,

I have the same problem,
this is my code snippet

//HttpServletResponse
getResponse().setContentType("application/vnd.ms-excel");
getResponse().setHeader("Content-disposition","attachment; filename=
"+theNameFile+".xls" );
getResponse().setHeader("Cache-Control"," private");

OutputStream unStream = null;
try {
  unStream = getResponse().getOutputStream();
  workbook.write(unStream); //IT CRASHES
  unStream.flush();
  unStream.close();
 ...

-->> I'm using poi-3.0-alpha1-20050704
But I obtain a NullPointerException

I tryed:

ByteArrayOutputStream byteOutS = new ByteArrayOutputStream();
wb.write(byteOutS);//IT CRASHES
byte[] excelBytes=byteOutS.toByteArray();

And once again NullPointerException

does it work only with FileOutputStream?

thanks
--
Adriana
p.d. sorry for my english... (ó_ò)

On 10/17/06, Brett Procek <b....@yahoo.com> wrote:
>
> Anil Komareddi <anil <at> effone.net> writes:
>
> >
> >
> > I'm trying to generate an excel spreadsheet and then email it as an
> > attachment. The generation part works fine. I can save this to a file
> > and view it. I can attach an existing spreadsheet file to an email and
> > send it and it goes out ok. The problem is when I try to generate the
> > spreadsheet and attach it without saving it first.
> >
> > This is what I'm doing:
> >
> > 1. Generate the workbook
> > 2. Get the data byte array from the workbook using
> > HSSFWorkbook.getBytes()
> > 3. Prepare a javax.activation.Datasource using this byte array where the
> > getInputStream method is as follows:
> >
> >     public InputStream getInputStream()
> >                                throws IOException
> >     {
> >         if (_data == null)
> >         {
> >             throw new IOException("no data");
> >         }
> >
> >         return new ByteArrayInputStream(_data);
> >     }
> >
> > 4. The emailer code itself, looks like this:
> >
> > "Lost Document Summary Information"
> >
>
> I had this same issue and figured out the problem.  Invoking
> HSSFWorkbook.getBytes() does not return all of the data necessary to re-
> construct a complete Excel file. An excel file itself contains more than
> the
> raw contents of the file.  The 'getBytes()' method apparently seems to
> miss
> other excell file meta data, including the file summary information, etc.
>
> Instead, in order to write the file out as an attachment, or stream, you
> must
> invoke the 'write()' method on the workbook itself, passing in a
> OutputSteam.
>
> - Brett
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
>
>


-- 
Adriana
--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.--.
Your grace has found me just as I am,
empty handed but alive in Your hands