开发者

email Excel file (in memory) as an attachment on Google app Engine

开发者 https://www.devze.com 2023-04-10 12:39 出处:网络
I googled and tried several approaches but failed. Here is the question: I try to create a excel file (using JExcelApi) and email it as an attachment on google app engine.

I googled and tried several approaches but failed. Here is the question: I try to create a excel file (using JExcelApi) and email it as an attachment on google app engine.

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    try {
        //write the excel file
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
        //first sheet
        WritableSheet sheet = workbook.createSheet("Param", 0);
        Label label11 = new Label(0, 0, "parameter is"); 
        sheet.addCell(label11);
        Label label12 = new Label(1, 0, "worker"); 
        sheet.addCell(label12);

        //second sheet
        WritableSheet sheet2 = workbook.createSheet("Info", 1);
        Label label21 = new Label(0, 0, "Info is"); 
        sheet2.addCell(label21);
        Label label22 = new Label(1, 0, "consumer"); 
        sheet2.addCell(label22);

        workbook.write(); 
        workbook.close();

        //email the excel file as an attachment
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("//my gmail", "Sr."));
        msg.addRecipient(Message.RecipientType.TO,
            new InternetAddress("//my gmail", "Mr. "));
        msg.setSubject("Your excel file is here");

        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();        
        htmlPart.setContent("please review", "text/html");
        mp.addBodyPart(htmlPart);

        MimeBodyPart attachment = new MimeBodyPart();
        attachment.setFileName("report.xls");
        attachment.setContent(outputStream.toByteArray(), "application/vnd.ms-exc开发者_StackOverflow社区el");              
        mp.addBodyPart(attachment);

        msg.setContent(mp);
        Transport.send(msg);

    } catch (RowsExceededException e) {
        /* foo */
    }
}

I uploaded but never a mail with the attached excel was sent to my mailbox. outputStream.toByteArray() may not be proper, but others I tried didn't work either.


Solve it.But anybody can explain it? Because some restriction of google app engine? Thanks.

DataSource src = new ByteArrayDataSource(outputStream.toByteArray()
                                       , "application/vnd.ms-excel");

attachment.setDataHandler(new DataHandler(src));
0

精彩评论

暂无评论...
验证码 换一张
取 消