利用java web工程,把用ireport生成的jasper文件,导出成pdf文件

2025-03-24 12:18:09
推荐回答(3个)
回答1:

在servlet里

	JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(保存数据的List对象);
File reportFile = null;
reportFile = new File(this.getServletContext().getRealPath("/report.jasper"));
FileInputStream fis = new FileInputStream(reportFile);
OutputStream os = null;
Map parameters = new HashMap();//报表要用的参数
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(fis, parameters, dataSource);
byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
response.setHeader("Content-Disposition", "attachment;filename=temp.pdf");
os = response.getOutputStream();
os.write(bytes);
os.flush();  
} catch (JRException e) {
e.printStackTrace();
}finally {
try {
if(os!=null)
os.close();
if(fis!=null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}

回答2:

可以参考一下两个博文:
http://www.open-open.com/lib/view/open1366080473734.html
http://www.tuicool.com/articles/eMfAra

回答3:

保存到文件就行了