通过电子邮件将 Excel 文件(在内存中)作为 Google App Engine 上的附件发送

发布于 2024-12-08 11:50:27 字数 2085 浏览 1 评论 0原文

我用谷歌搜索并尝试了几种方法但都失败了。 问题是:我尝试创建一个 Excel 文件(使用 JExcelApi)并将其作为附件通过电子邮件发送到 google 应用引擎。

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-excel");              
        mp.addBodyPart(attachment);

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

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

我上传了,但从未将带有 Excel 附件的邮件发送到我的邮箱。 outputStream.toByteArray() 可能不正确,但我尝试的其他方法也不起作用。

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-excel");              
        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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虫児飞 2024-12-15 11:50:27

解决了。​​但是有人能解释一下吗?因为谷歌应用引擎的一些限制?谢谢。

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

attachment.setDataHandler(new DataHandler(src));

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文