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
                 

Reply via email to