Im with a different problem... I've googled a little but havent found anything about my problem so im asking here... I have an object JasperPrint where i generate the document... The problem is that i need to create a java开发者_JAVA技巧.io.File from this JasperPrint without saving the file on the computer.
What do i need to do is: send a file by email. And this file must be generated by the jasperreport. I can't save the stream on the machine to delete it later... so i need to take the file in memory or something like that in runtime...
So... i have my object jasperprint and need to get a java.io.File from this one... Someone knows what do can i do?
Andrew... couldnt answer it at comment so im writing it here... In javax.mail i've done like this:
File fileAttachment = myfile;
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileAttachment.getName());
multipart.addBodyPart(messageBodyPart);
and its working when i pass him a file from my machine... So i think its gonna work when i use a java.io.File even if its only on memory...
You can generate the report as PDF(or other format) and send it as a file with Jasper. JRXlsExporter
some snippet:
JasperPrint print = JasperFillManager.fillReport(report, new HashMap(), jasperReports);
long start = System.currentTimeMillis();
OutputStream output = new FileOutputStream(new File("c:/output/JasperReport.pdf"));
JasperExportManager.exportReportToPdfStream(print, output);
// coding For Excel:
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JA SPER_PRINT, print);
exporterXLS.setParameter(JRXlsExporterParameter.OU TPUT_STREAM, output);
exporterXLS.setParameter(JRXlsExporterParameter.IS _ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS _AUTO_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS _WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS _REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.exportReport();
You can write it to a OutputStream
and then use this stream to create the e-mail attachement. Here is an example for a XLS exporter.
JasperPrint jsPrint;
ByteArrayOutputStream out = new ByteArrayOutputStream();
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jsPrint);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, out);
exporterXLS.exportReport();
If you want to send this, you can create a ByteArrayDataSource
(see your updated question) instead of a FileDataSource
:
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ByteArrayDataSource bads = new ByteArrayDataSource(in,mimeType);
Do you want to send it as a .print file or something universally readable like .pdf?
I don't think it matters, though. The issue here isn't with JasperPrint, it's with the java*x* mail classes, which have a steep learning curve. Look particularly at javax.mail.internet.MimeMultipart
.
精彩评论