http://jakarta.apache.org/poi/javadocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html#getBytes()
"Method getBytes - get the bytes of just the HSSF portions of the XLS
file. Use this to construct a POI POIFSFileSystem yourself."
Meaning you're getting ONLY the "Workbook" steam in the XLS file. You'd
need to use POIFS to create a whole XLS file.
To do what you're attempting, construct an output stream, pass it to
HSSFWorkbook.write(OutputStream), read the output stream into a byte
array and construct an input stream around that.
Does that help?
-Andy
Anil Komareddi wrote:
> 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:
>
> byte[] data = handler.getExcelBytes();
> XLSDataSource dataSource = new XLSDataSource(data);
> try
> {
>
> MimeBodyPart bodyPart = new MimeBodyPart();
> bodyPart.setHeader("Content-Disposition",
> "attachment;filename=" +
>filename);
> bodyPart.setHeader("Content-Type",
>"application/x-excel");
> bodyPart.setDataHandler(new
>DataHandler(dataSource));
> bodyPart.setFileName(filename);
>
> String to = "[EMAIL PROTECTED]";
> String from = "[EMAIL PROTECTED]";
> String host = "";
> String msgText1 = "Sending an xls attachment.\n";
> String subject = "xls test";
>
> Properties props = System.getProperties();
> props.put("mail.smtp.host", host);
>
> Session session = Session.getDefaultInstance(props,
>null);
>
> MimeMessage msg = new MimeMessage(session);
> msg.setFrom(new InternetAddress(from));
> InternetAddress[] address = {new
>InternetAddress(to)};
> msg.setRecipients(Message.RecipientType.TO,
>address);
> msg.setSubject(subject);
>
> MimeBodyPart mbp1 = new MimeBodyPart();
> mbp1.setText(msgText1);
>
> Multipart mp = new MimeMultipart();
> mp.addBodyPart(mbp1);
> msg.setContent(mp);
> msg.setSentDate(new Date());
> Transport.send(msg);
>
> } catch (MessagingException mex) {
> mex.printStackTrace();
> }
>
>
> Now this code works fine to some extent in that the email goes
>through with the Excel attachment. When I open the attachment in Excel
>XP/2002, the spreadsheet opens fine and all the data is there, but I get
>an error dialog which says:
>
> -----------
> Microsoft Excel File Repair Log
>
> Errors were detected in file 'C:\Documents and
>Settings\anil\Local Settings\Temporary Internet Files\OLKA9\attach.xls'
> The following is a list of repairs:
>
> Lost document summary information.
> -----------
>
> If I try to open the attachment in any previous version of Excel
>or StarOffice, it opens a blank workbook.
>
> Hope this helps to provide enough information on the problem.
>
> Thanks,
> Anil Kommareddi
>
>
>
>
>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>