gpt4 book ai didi

java - 将 JasperPrint 转换为文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:37 29 4
gpt4 key购买 nike

我有一个不同的问题......我用谷歌搜索了一下但没有找到任何关于我的问题的信息所以我在这里问......我有一个对象 JasperPrint,我在其中生成文档...问题是我需要从这个 JasperPrint 创建一个 java.io.File 而不是在计算机上保存文件。

我需要做的是:通过电子邮件发送文件。而且这个文件必须是jasperreport生成的。我无法将流保存在机器上以便稍后将其删除...所以我需要在运行时将文件放入内存或类似的东西...

所以...我有我的对象 jasperprint,需要从这个对象中获取一个 java.io.File...有人知道我能做什么吗?

安德鲁...无法在评论中回答,所以我写在这里...在 javax.mail 中我这样做了:

File fileAttachment = myfile;
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileAttachment.getName());
multipart.addBodyPart(messageBodyPart);

当我从我的机器上向他传递文件时,它正在工作...所以我认为当我使用 java.io.File 时它会起作用,即使它只在内存中......

最佳答案

您可以将报告生成为 PDF(或其他格式)并使用 Jasper 将其作为文件发送。JRXlsExporter

一些片段:

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();

关于java - 将 JasperPrint 转换为文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6347325/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com