如何通过电子邮件发送Excel导出器插件的输出

时间:2015-06-03 11:49:06

标签: excel email grails export

我们正在使用此插件生成Excel报告:https://github.com/TouK/excel-export并将此插件邮寄给我们:https://grails.org/plugin/mail

问题是如何将Excel导出的输出作为附件发送到邮件中,而不必将其作为文件保存到磁盘(因为我们有数千个,并且不知道我们是否可以写到FS等)。

我们正在猜测:

    OutputStream outputStream = new ByteArrayOutputStream()

    def withProperties = ['name', 'description']
    List<String> products = ['john', 'lazy']
    new XlsxExporter().add(products, withProperties).save(outputStream)

    sendMail {
        mutipart true
        async true
        to "hello@me.com"
        subject reportSchedule.report.name
        from "reports@me.com"
        body 'Report: '  + reportSchedule.report.name + " took: 20s"
        attach "yourfile.txt", "text/plain", inputStream
    }

问题是,我们如何将输出流从Excel导出器连接到邮件插件所需的输入流?

还试过这个:

    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray())

    sendMail {
        multipart true
        async true
        to "hello@me.com"
        subject reportSchedule.report.name
        from "hello@me.com"
        body 'Report: '  + reportSchedule.report.name + " took: 20s"
        attach "yourfile.txt", "text/plain", inputStream
    }

但这给出了:

attach() is applicable for argument types: (java.lang.String, java.lang.String, java.io.ByteArrayInputStream)

和此:

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream()

    def withProperties = ['name', 'description']
    List<String> products = ['john', 'lazy']
    new XlsxExporter().add(products, withProperties).save(outputStream)

    sendMail {
        multipart true
        async true
        to "me@me.com"
        subject reportSchedule.report.name
        from "me@me.com"
        body 'Report: '  + reportSchedule.report.name + " took: 20s"
        attach "report.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", outputStream.toByteArray()
    }

这会将.xlsx文件附加到大小约3k的电子邮件中,但它是空白的,完全为空。

1 个答案:

答案 0 :(得分:0)

好的问题解决了。正确的方法是:

OutputStream outputStream = new ByteArrayOutputStream()

def withProperties = ['name', 'description']
List<Product> products = ProductFactory.getSome()
new XlsxExporter().add(products, withProperties).save(outputStream)

sendMail {
    multipart true
    async true
    to "me@me.com"
    subject reportSchedule.report.name
    from "me@me.com"
    body 'Report: '  + reportSchedule.report.name + " took: 20s"
    attach "report.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", outputStream.toByteArray()
}

我出错的地方只是产品需要是具有与标题匹配的字段的复杂对象,而不仅仅是字符串。