我们正在使用此插件生成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的电子邮件中,但是它是空白的,完全为空。
最佳答案
好的问题解决了。正确的方法是:
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()
}
我要弄错的地方仅仅是产品必须是具有与标题匹配的字段的复杂对象,而不仅仅是字符串。
关于excel - 如何通过电子邮件发送Excel Exporter插件的输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30619253/