Convert JasperPrint to file

Im having a different problem ... I was a bit googled looking, but havent found anything about my problem, so I ask here ... I have a JasperPrint object where I create a document ... The problem is that I need to create java.io.File from this JasperPrint without saving the file to the computer.

What I need to do: send the file by email. And this file should be generated by jasperreport. I cannot save the stream on the machine to delete it later ... so I need to take a file in memory or something similar at runtime ...

So ... I have my jasperprint object and you need to get java.io.File from this ... Does anyone know what I can do?

Andrew ... could not reply to the comment, so I wrote it here ... In javax.mail I did 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 work when I transfer the file to it from my machine ... So I think it will work when I use java.io.File, even if it is only in memory ...

+3
source share
3 answers

You can generate the report as a PDF (or other format) and send it as a file using Jasper. JRXlsExporter

some fragment:

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(); 
+3
source

You can write it to OutputStream, and then use this stream to create email attachments. Here is an example for an 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 message, you can create ByteArrayDataSource(see updated question) instead of FileDataSource:

ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ByteArrayDataSource bads = new ByteArrayDataSource(in,mimeType);
+1
source

.print - , .pdf?

, . JasperPrint, java * x * mail , . javax.mail.internet.MimeMultipart.

0

All Articles